之前我问过这个问题,但我更新了我的代码,同样的问题也随之增加。当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);
我不知道自己做错了什么。如果需要更多代码,我可以提供。
答案 0 :(得分:2)
根据this Unity答复帖子,
看起来您仍然锁定了您尝试写入的文件。我认为它可能与您在尝试使用StreamWriter访问它之前调用的File.Create()方法有关。
哪个匹配代码的这一位和错误位置:
using (File.CreateText(pathToLoad))
{
using (TextWriter writer = new StreamWriter(pathToLoad, false))
{
所以你可能需要遵循相同的建议并在将Dispose()
传递给编写者之前调用File.Create("filepath").Dispose();
:
using
旁注:该行上的File.CreateText(pathToLoad)).Dispose();
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();
}
可能也不合适。您实际上并没有声明要使用的变量,而是调用一个方法来返回您忘记的对象。这可能会更好:
int array[] = {23,34,12,17,204,99,16};
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
printf("%d",sizeof(TOTAL_ELEMENTS));