【问题标题】:The process cannot access the file because it is being used (error)该进程无法访问该文件,因为它正在被使用(错误)
【发布时间】:2012-05-02 10:25:50
【问题描述】:

我试图将文本文件中的行向上移动一个然后将其重写回原始文件,但由于某种原因出现错误,似乎无法弄清楚。

using (StreamReader reader = new StreamReader("file.txt"))
{
    string line;
    int Counter = 0;

    while ((line = reader.ReadLine()) != null)
    {
        string filepath = "file.txt";
        int i = 5;
        string[] lines = File.ReadAllLines(filepath);

        if (lines.Length >= i)
        {
            string tmp = lines[i];
            lines[i] = lines[i-1];
            lines[i-1] = tmp;
            File.WriteAllLines(filepath, lines);
        }   
    }
    Counter++;
}

【问题讨论】:

  • 嗯..我认为你在这里做的有点疯狂......这个问题是你试图写入你已经在 StreamReader 中打开的文件。你到底想做什么,明确的?也许我们可以帮助您解决您的问题。
  • 可能将所有文件内容存储在数组或列表中。移动它们然后将整个东西存储回来..没有while循环
  • 你想交换文件中的每一行吗?
  • @Tim Schmelter,我只交换文件中的一行
  • @user1285872:我已编辑 my answer below 以考虑到这一点。

标签: c# .net visual-studio c#-4.0


【解决方案1】:

您正在打开要在此行中读取的文件:

using (StreamReader reader = new StreamReader("file.txt"))

此时它已打开并被使用。

那么,你以后有:

string[] lines = File.ReadAllLines(filepath);

试图从 same 文件中读取。

目前尚不清楚您要达到什么目的,但这不起作用。

据我所知,您根本不需要reader

【讨论】:

    【解决方案2】:

    我假设您实际上想要交换文件中的每一行(?),因为这段代码 sn-p:

    string tmp = lines[i];
    lines[i] = lines[i-1];
    lines[i-1] = tmp;
    

    所以这是一种应该可行的方法:

    String[] lines = System.IO.File.ReadAllLines(path);
    List<String> result = new List<String>();
    for (int l = 0; l < lines.Length; l++)
    {
        String thisLine = lines[l];
        String nextLine = lines.Length > l+1 ? lines[l + 1] : null;
        if (nextLine == null)
        {
            result.Add(thisLine);
        }
        else
        {
            result.Add(nextLine);
            result.Add(thisLine);
            l++;
        }
    }
    System.IO.File.WriteAllLines(path, result);
    

    编辑:这是稍作修改的版本,仅与前一行交换了一行,因为您已评论说这是您的要求:

    String[] lines = System.IO.File.ReadAllLines(path);
    List<String> result = new List<String>();
    int swapIndex = 5;
    if (swapIndex < lines.Length && swapIndex > 0)
    {
        for (int l = 0; l < lines.Length; l++)
        {
            String thisLine = lines[l];
            if (swapIndex == l + 1) // next line must be swapped with this
            {
                String nextLine = lines[l + 1];
                result.Add(nextLine);
                result.Add(thisLine);
                l++;
            }
            else
            {
                result.Add(thisLine);
            }
        }
    }
    System.IO.File.WriteAllLines(path, result);
    

    【讨论】:

      【解决方案3】:

      您尝试打开文件以写入文件的位置是在一个已经使用流读取器打开它的方法中,流读取器打开它,文件写入器尝试打开它但因为它已经打开而无法打开,

      【讨论】:

        【解决方案4】:

        不要同时读取和写入文件 1.如果文件很小,只需加载它,更改它,然后写回。 2.如果文件很大,只需打开另一个临时文件进行输出, 删除/删除第一个文件,然后重命名第二个文件。

        【讨论】:

          【解决方案5】:

          而不是:

          using (StreamReader reader = new StreamReader("file.txt"))
          {
          ...
          string[] lines = File.ReadAllLines(filepath);
          }
          

          用途:

          using (StreamReader reader = new StreamReader("file.txt"))
          {
          string line;
          string[] lines = new string[20]; // 20 is the amount of lines
          int counter = 0;
          while((line=reader.ReadLine())!=null)
          {
              lines[counter] = line;
              counter++;
          }
          }
          

          这将从文件中读取所有行并将它们放入“行”中。

          您可以对代码的编写部分执行相同的操作,但这种方式仅使用 1 个进程从文件中读取。它将读取所有行,然后自行处理和关闭。

          希望对您有所帮助!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-23
            • 2019-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-10
            相关资源
            最近更新 更多