【问题标题】:File sharing violation occurs after creation of file?创建文件后发生文件共享冲突?
【发布时间】:2012-12-28 02:37:33
【问题描述】:

所以,我正在尝试创建一个 .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;
    }

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

【问题讨论】:

  • 可能是 File.Create 保持打开状态?我总是让 StreamWriter 创建文件。

标签: c# text streamreader sharing streamwriter


【解决方案1】:

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

从上面的链接:

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

【讨论】:

  • 太棒了。我会试试的。干杯。
  • 辛苦了。谢谢马克。
【解决方案2】:

StreamWriter 无法访问该文件,因为 File.Create 返回了一个 FileStream 你没有消费。

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

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

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

【讨论】:

    【解决方案3】:

    这可能是因为StreamWriter 尚未释放非托管资源: 尝试使用using的块,而不是手动打开和关闭它。

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

    【讨论】:

      【解决方案4】:

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

      System.IO.IOException
      在路径 /.../... 上共享冲突

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

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

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

      File.AppendAllText(path, str);
      

      如果文件已经存在,程序将打开该文件并将文本附加到它的末尾,如果它不存在将创建它并附加文本。

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

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

      【讨论】:

        【解决方案5】:

        多线程编程我也面临同样的问题,我认为它的线程同步问题,但问题是在 File.Create() 之后我使用了 StreamWriter。在这里,您可以尝试而不是使用两个不同的步骤,您可以在一个步骤中组合。这段代码对我来说很好。

        using (var writer = new StreamWriter(File.Exists(path) ? File.Open(path, FileMode.Open) : File.Create(path)))
        {
            writer.Write(str);
            writer.Close();
        }
        

        【讨论】:

        • 这似乎没有回答共享冲突问题的问题。
        猜你喜欢
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 2021-11-01
        • 2013-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多