【问题标题】:The process cannot access the file 'path' because it is being used by another process该进程无法访问文件“路径”,因为它正被另一个进程使用
【发布时间】:2014-05-08 15:15:31
【问题描述】:

我正在尝试使用以下代码在 C# 应用程序中读取对文本文件的写入

using (System.IO.StreamReader rd = new System.IO.StreamReader(path))
{
    using (System.IO.StreamWriter wr = new System.IO.StreamWriter(path))
    {
        string line = null;
        while ((line = rd.ReadLine()) != null)
        {
            if (String.Compare(line, active_user) == 0)
            {
               //do nothing
            }
            else
            {
                wr.WriteLine(active_user);
            }

        }
    }                                     
}

即使我使用一次性物品,仍然给我报错

该进程无法访问文件“路径”,因为它正被另一个进程使用。

我是不是哪里出错了?

【问题讨论】:

  • 我正在检查所有内容,但没有找到任何使用该文件的应用程序。有没有办法强制所有进程以编程方式释放文件?
  • 你正在同时读取和写入文件。
  • 这是错误的方法。写入一个新的临时文件,然后删除旧文件并将新文件重命名为旧名称。

标签: c# visual-studio-2012 text-files


【解决方案1】:

您正在从变量path 中定义的路径读取文件,同时尝试写入它。

【讨论】:

  • 要添加到这个答案,您可以同时使用 StreamReader 和 StreamWriter,但您必须使用相同的流。在构造函数中使用路径字符串意味着正在为每个创建单独的内部流。首先创建您的流,然后在您的读取器/写入器构造函数中使用它。
  • @NathanA 最好的解决方法?
  • 我还建议你不要尝试这个。即使您同时阅读和写作,您的整个概念也不会起作用。流中的指针一次只能指向文件中的一个点。我建议您将所有需要的内容写入一个新文件,关闭它,然后删除旧文件并重命名新文件以替换旧文件。会干净很多。
  • @NathanA 不是真的,他可以同时读写(同一时间意味着一次通过,按顺序完成,不完全在同一时刻),但他必须定位到指针每次操作前正确。
  • @kayson 是的,这就是我的意思。我确实提到这是可能的。不过,鉴于他的示例,如果不通过指针调整使其变得更加复杂,它就不会像他预期的那样工作。
【解决方案2】:

您正在同时读取和写入文件。试着列出你需要写的字符串,读完后再写。

类似这样的:

List<string> toWrite = new List<string>();

using (System.IO.StreamReader rd = new System.IO.StreamReader(path))
{
    string line = null;
    while ((line = rd.ReadLine()) != null)
    {
        if (String.Compare(line, active_user) != 0) //simplified your logic
        {
            toWrite.Add(active_user);
        }

    }

}
File.WriteAllLines(path, toWrite);

【讨论】:

    【解决方案3】:

    在写入时,阅读器仍在范围内并访问文件,这就是您收到此错误的原因。

    【讨论】:

    • 问题不在于读者。问题是阅读器以不必要的阻塞模式打开了文件。
    【解决方案4】:

    当读取和写入同一个文件时,不要在两个不同的实例中打开它。

    ...就像打开车门让两个人上车一样。 您只能打开一次 - 您需要先关闭才能再次打开它。 但是,您可以让两者都进入。

    在以下示例中: 您打开流(一次) 然后让作者和读者可以访问打开文件的单个实例。 当它们都完成后,您就可以关闭文件了。

    注意:在像多线程这样更复杂的场景中 - 您需要实现线程安全以防止读取器和写入器同时尝试读取和写入。 ...但是对于您的示例,以下内容就足够了。

            using (System.IO.Stream stream = System.IO.File.Open(path, System.IO.FileMode.Open))
            {
                using (System.IO.StreamReader rd = new System.IO.StreamReader(stream))
                {
                    using (System.IO.StreamWriter wr = new System.IO.StreamWriter(stream))
                    {
                        string line = null;
                        while ((line = rd.ReadLine()) != null)
                        {
                            if (String.Compare(line, active_user) == 0)
                            {
                                //do nothing
                            }
                            else
                            {
                                wr.WriteLine(active_user);
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      【解决方案5】:

      尽管有其他帖子,但“同时”读取文件并写入文件并没有错。理解问题的关键是打开文件时使用的模式。

      看看你的代码更明确的变体:

              using (FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
              {
                  using (StreamReader rd = new StreamReader(fileStream))
                  {
                      using (StreamWriter wr = new StreamWriter(fileStream))
                      {
                          string line = null;
                          while ((line = rd.ReadLine()) != null)
                          {
                              if (String.Compare(line, active_user) == 0)
                              {
                                  //do nothing
                              }
                              else
                              {
                                  wr.WriteLine(active_user);
                                  //If you flush what you write then you will see it via the reader.
                                  //Comment out for better performance. Keep in mind, that writing
                                  //into a file will lead to occasional flush which affects the
                                  //reader. Implicit Flush happens once you write more than the
                                  //buffer of the writer suggests.
                                  wr.Flush();
                              }
      
                          }
                      }
                  }                 
              }
      

      如您所见,打开文件的方式与您的代码不同。我只是更喜欢明确指定我打开文件的模式。 StreamReader 似乎在没有“共享”的情况下隐式打开文件,即不允许其他人打开它,这没关系,因为它想保护您免受意外结果。因此,这种隐式方法不适合您稍微“复杂”的场景。显式打开文件可以解决问题。 请注意,File.Open 允许您打开文件,但仍允许其他线程(其他应用程序)打开该文件。这完全取决于你。

      现在来看看你的代码逻辑。 StreamReader 从头开始​​读取流。 StreamWriters 附加到流内容的末尾。两者都保持并保持自己的流位置。因此,代码将为文件中不包含“active_user”的每一行附加一个包含“active_user”的行。我无法想象一个实际的原因。

      【讨论】:

        猜你喜欢
        • 2022-11-18
        • 2021-12-02
        • 2010-12-10
        相关资源
        最近更新 更多