创建文件后是否发生文件共享冲突?

时间:2013-01-14 04:19:59

标签: c# text streamreader sharing streamwriter

所以,我正在尝试创建一个.txt文件,然后向它写一些愚蠢的数据。但我收到了共享违规行为。我觉得这可能是因为我试图在创建文件后直接为文件创建一个StreamWriter,但这没有意义。所以我有点迷茫。我已经尝试删除类中除了错误行之外的所有其他StreamWriters和Readers,我仍然会受到违规。我得到的错误,

IOException: Sharing violation on path C:\Users\USER\Desktop\Accessible\Assets\IO\Books\A community of learners\frog\frog_status.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options)
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)

指向该行:

StreamWriter sw = new StreamWriter(statusTxtPath);

在以下功能中:

private IEnumerator GatherStatusInfo(string folderPath, string mediaName) {

        string statusTxtPath = folderPath + @"/" + mediaName + "_status.txt";
        _currentInfoBeingCreated._readingStatusTxtPath = statusTxtPath;

        if(BuildManager.instance._isResetStatusFilesWhenStarting) {
            if(File.Exists(statusTxtPath)) 
                File.Delete(statusTxtPath);
        }

        if(!File.Exists(statusTxtPath)) {
            File.Create(statusTxtPath);
            StreamWriter sw = new StreamWriter(statusTxtPath);
            sw.WriteLine(MediaStatus.Unread);
            sw.WriteLine("0");
            _currentInfoBeingCreated._status = MediaStatus.Unread;
            _currentInfoBeingCreated._pageLastRead = 0; 
            sw.Flush();
            sw.Close();

        } else {

            StreamReader statusReader = new StreamReader(statusTxtPath);
            _currentInfoBeingCreated._status = (MediaStatus) Enum.Parse(typeof(MediaStatus), statusReader.ReadLine());
            _currentInfoBeingCreated._pageLastRead = (int) ConversionUtils.instance.String2Float(statusReader.ReadLine());
            statusReader.Close();
        }

        yield return 0;
    }

知道为什么那条狗不会打猎?

5 个答案:

答案 0 :(得分:3)

您是否有理由想要创建该文件然后重新打开以写入该文件。 StreamWriter有一种方法可以做到这一点。如果它不存在,它将创建一个新文件。

从上面链接:

  

使用默认编码和缓冲区大小为指定路径上的指定文件初始化StreamWriter类的新实例。如果文件存在,则可以覆盖或附加。如果该文件不存在,则此构造函数将创建一个新文件。

答案 1 :(得分:3)

StreamWriter无法访问该文件,因为File.Create返回了您未消费的FileStream

如上所述,File.Create不是必需的。你也可以使用:

using (var writer = new StreamWriter(File.Create(statusTxtPath)))
{
   // do work here.
}

将使用文件流并关闭它。每当使用流和大多数与流交互的类时,请务必使用using()块来确保正确释放句柄。

答案 2 :(得分:1)

可能会发生这种情况,因为StreamWriter尚未释放未受管理的资源: 尝试使用using块而不是手动打开和关闭它。

using (StreamWriter writer = new StreamWriter(statusTxtPath))
    {
        // do work here
    }

答案 3 :(得分:1)

我是这种编程语言的新手,我知道一些Ansi C主要来自编程微控制器,但我现在正在学习C#,我遇到了同样的问题。

  

System.IO.IOException
  在路径上共享违规/.../...

我发现使用以下代码会引发异常。

if(!File.Exists(path))
   File.Create(path);

我发现解决这个问题的解决方案实际上使用的代码更少,更简单。

File.AppendAllText(path, str);

使用此行,程序将打开文件(如果文件已存在)并将文本附加到文件末尾,如果文件不存在则创建文件并附加文本。

存在:路径 - >文件的路径
STR - >要写入文件的文本

我在this Xamarin论坛页面上找到了解决方案的提示。

答案 4 :(得分:-1)

我也遇到了与多线程编程相同的问题,我想它的线程同步问题,但问题是在File.Create()之后我使用了StreamWriter。在这里,您可以尝试而不是使用两个不同的步骤,您可以一步完成。这段代码对我来说很好。

grep 'substring1' file1.txt > outfile.txt ; grep 'substring2' file2.txt >> outfile.txt