C# 在unity中编写txt文件时,它表示路径上存在共享冲突

C# 在unity中编写txt文件时,它表示路径上存在共享冲突,c#,unity3d,scripting,save,C#,Unity3d,Scripting,Save,我以前问过这个问题,但我更新了代码,同样的问题也出现了。当路径toload(Application.persistentDataPath+[save name])中已经存在保存文件时,脚本可以完美地工作。。这是我的密码: 这是保存/加载代码: public void GetDatesFromFile(string pathToLoad) { Debug.Log("It is here!"); if (File.Exists(pathToLoad)) { D

我以前问过这个问题,但我更新了代码,同样的问题也出现了。当
路径toload(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";
        }
    }
}
这是MainMenuBasicBehavior的主要类

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;

    }
}
它指向的线是:

SavingTextsScripts.cs:168 is the line: using (TextWriter writer = new StreamWriter(pathToLoad, false))
MainMenuBasicBehaviour.cs:33 is the line: savingTextObject.GetDatesFromFile(filePath);
我不知道我做错了什么。如果需要更多的代码,我可以提供。

根据Unity Answer post

看起来您仍锁定了要写入的文件。我认为这可能与您在尝试使用StreamWriter访问File.Create()方法之前调用的File.Create()方法有关

这与您的代码的这一位和错误位置相匹配:

using (File.CreateText(pathToLoad))
{
    using (TextWriter writer = new StreamWriter(pathToLoad, false))
    {
因此,在将其传递给streamwriter之前,您可能需要遵循相同的建议并调用
Dispose()

File.Create("filepath").Dispose();
旁注:
在该行中使用
可能也不合适。实际上,您并不是在声明要使用的变量,而是在调用一个方法,该方法返回一个对象,而您却忘记了这个对象。这样做可能更好:

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();
}
据统一报报道,

看起来您仍锁定了要写入的文件。我认为这可能与您在尝试使用StreamWriter访问File.Create()方法之前调用的File.Create()方法有关

这与您的代码的这一位和错误位置相匹配:

using (File.CreateText(pathToLoad))
{
    using (TextWriter writer = new StreamWriter(pathToLoad, false))
    {
因此,在将其传递给streamwriter之前,您可能需要遵循相同的建议并调用
Dispose()

File.Create("filepath").Dispose();
旁注:
在该行中使用
可能也不合适。实际上,您并不是在声明要使用的变量,而是在调用一个方法,该方法返回一个对象,而您却忘记了这个对象。这样做可能更好:

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();
}

尝试
Application.persistentDataPath+System.IO.Path.directoryseportorchar+filepath[i]
System.IO.Path.Combine(Application.persistentDataPath,filepath[i])
并记住首先检查要存储文件的任务。您应该考虑在用户文档中或在AdDATA文件夹下使用一个文件夹来保存数据。[
Application.persistentDataPath+System.IO.Path.directorysepertorchar+@“Saves/Save.txt”
],但出现了相同的错误。文件就位后,没有问题,但如果没有问题,则显示相同的IOException
Application.persistentDataPath+System.IO.Path.directorySepertorchar+filepath[i]组合(Apvest.RealthDATaPATH,FieleApths[i]);,并记住检查第一次存储文件。您应该考虑在用户文档或AdDATA文件夹下保存文件夹的数据。(Application.persistentDataPath+System.IO.Path.directorysepartorchar+“Saves”)
],现在将从[
Application.persistentDataPath+System.IO.Path.directorysepartorchar+@“Saves/Save.txt”保存/加载保存,但出现了相同的错误。文件就位后,没有问题,但如果没有问题,则会显示相同的IOException。这是问题所在,现在工作正常。非常感谢!这是问题所在,现在工作正常。非常感谢!