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

时间:2014-05-08 15:15:31

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

我正在尝试使用以下代码读取对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);
            }

        }
    }                                     
}

即使我使用的是一次性物品,仍然向我提出错误

  

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

我在哪里出错了?

5 个答案:

答案 0 :(得分:2)

在尝试同时写入变量path时,您正在读取变量{{1}}中定义的路径。

答案 1 :(得分:1)

您正在同时阅读和写入该文件。尝试列出你需要写的字符串,然后在阅读完毕后写下它们。

这样的事情:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

        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”。我无法想象这是一个实际的原因。

答案 4 :(得分:0)

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

...就像为两个人打开车门一样。 您只能打开一次 - 您需要关闭才能再次打开它。 但是,您可以将它打开以便双方进入。

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

注意:在更复杂的情况下,如多线程 - 您需要实现线程安全性,以防止读写器同时尝试读写。 ......但是对于你的例子,下面就足够了。

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