【问题标题】:When writing a txt file in unity, it says sharing violation on path统一编写txt文件时,它说在路径上共享冲突
【发布时间】: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

标签: c# unity3d scripting save


【解决方案1】:

根据thisUnity 回复帖,

您尝试写入的文件似乎仍处于锁定状态。而且我认为这可能与您在尝试使用 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();
}

【讨论】:

  • 这是问题所在,现在完美运行。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 2021-06-23
  • 2016-02-24
  • 1970-01-01
相关资源
最近更新 更多