【发布时间】:2017-11-23 04:37:44
【问题描述】:
我之前问过这个问题,但我更新了我的代码,同样的问题出现了。当pathToLoad(Application.persistentDataPath+[save name]) 中已有保存文件时,该脚本可以完美运行。这是我的代码:
这是保存/加载代码:
public void GetDatesFromFile(string pathToLoad)
{
Debug.Log("It is here!");
if (File.Exists(pathToLoad))
{
Debug.Log("Exists");
using (StreamReader reader = File.OpenText(pathToLoad))
{
string currentLine = "";
string[] currentElements;
string[] separatorString = new string[] { "," };
currentLine = reader.ReadLine();
currentElements = currentLine.Split(separatorString, StringSplitOptions.RemoveEmptyEntries);
MainMenuBasicBehaviour.theTime = currentElements[0];
MainMenuBasicBehaviour.theDate = currentElements[1];
reader.Close();
}
}
else
{
Debug.Log("No Exists");
using (File.CreateText(pathToLoad))
{
using (TextWriter writer = new StreamWriter(pathToLoad, false))
{
writer.WriteLine("00:00:00,00/00/0000,1,1,500,20000,1500,50,20,10,5,2,1,1,0,10,50,50");
writer.Close();
}
MainMenuBasicBehaviour.theTime = "00:00:00";
MainMenuBasicBehaviour.theDate = "00 / 00 / 0000";
}
}
}
这是主类 MainMenuBasicBehaviour
public void GetDates()
{
for (int i = 0; i < filePaths.Length; i++)
{
filePath = Application.persistentDataPath.ToString() + filePaths[i];
Debug.Log(theDate + " " + theTime + filePath);
savingTextObject.GetDatesFromFile(filePath);
Debug.Log(theDate+" "+theTime+filePath);
SavingTexts[i].text = "Load game taken in:\n " + theDate + " at " + theTime;
}
}
错误如下:
IOException: Sharing violation on path C:\*\The Age Of Politics\Autosave.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path, Boolean append)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
SavingTextsScripts.GetDatesFromFile (System.String pathToLoad) (at Assets/Scripts/SavingTextsScripts.cs:168)
MainMenuBasicBehaviour.Start () (at Assets/Scripts/MainMenuBasicBehaviour.cs:33)
它指向的行是:
SavingTextsScripts.cs:168 is the line: using (TextWriter writer = new StreamWriter(pathToLoad, false))
MainMenuBasicBehaviour.cs:33 is the line: savingTextObject.GetDatesFromFile(filePath);
我不知道我做错了什么。如果需要更多代码,我可以提供。
【问题讨论】:
-
尝试
Application.persistentDataPath + System.IO.Path.DirectorySeparatorChar + filePaths[i];或System.IO.Path.Combine(Application.persistentDataPath, filePaths[i]);并记得先检查存储文件的权限。您应该考虑在用户文档或 AddData 文件夹下创建一个用于保存数据的文件夹。 -
@m.rogalski 我用 [
Directory.CreateDirectory(Application.persistentDataPath + System.IO.Path.DirectorySeparatorChar + "Saves")] 创建了一个文件夹,现在将从 [Application.persistentDataPath + System.IO.Path.DirectorySeparatorChar+ @"Saves/Save.txt"] 保存/加载存档,但出现了同样的错误。文件就位,没有问题,但没有时,它显示相同的IOException