【问题标题】:File cannot be accessed because it is being used by another process文件无法访问,因为它正被另一个进程使用
【发布时间】:2015-08-12 15:57:31
【问题描述】:

我对编码有点陌生,我一直在尝试替换文本文件中的一个单词,但是当我执行程序时,它给了我“文件被另一个进程错误使用”

private void btnSave1_Click(object sender, EventArgs e)
        {
            string DOB = dateTimePicker1.Value.ToString();
            string Fname = txtBFirstName.ToString();
            string Lname = txtBLastName.ToString();
            string IDnum = txtBIDnum.ToString();
            string Address = txtBAddress.ToString();
            string nationality = txtBNationality.ToString();
            //string gender = cmbGender.SelectedItem.ToString();
           // string marrStatus = cmbMaritialStatus.SelectedItem.ToString();
            StreamReader read = null;

            //write to file
            try
            {
               // var lines = File.ReadAllLines("CV.txt");
                string line;
                read = new StreamReader("CurriculumVitae.txt");

                while ((line = read.ReadLine()) != null) 
                {
                    string text = File.ReadAllText("CurriculumVitae.txt");

                    text = text.Replace("Empty", DOB);
                    File.WriteAllText("CurriculumVitae.txt",
                                       File.ReadAllText("CurriculumVitae.txt")
                                       .Replace("empty",DOB));

                }



            }
            catch (Exception exc)
            {

                MessageBox.Show(exc.Message);
            }
            finally
            {
                read.Close();
            }

                //Open Next Form
                Education objNextForm = new Education();
                objNextForm.Visible = true;

}

【问题讨论】:

  • 哦,请忽略我的问题下的文本,这是一个无辜的错误
  • 你为什么要逐行阅读,但然后 ReadAllText?这没有任何意义。您还阅读了文件再次执行替换再次

标签: c#


【解决方案1】:

这3行的问题

read = new StreamReader("CurriculumVitae.txt");

string text = File.ReadAllText("CurriculumVitae.txt");

File.WriteAllText("CurriculumVitae.txt"
        ,File.ReadAllText("CurriculumVitae.txt").Replace("empty",DOB));

StreamReader 和 File.ReadAllText 都会锁定一个文件。并且每当他们尝试锁定同一个文件时都会出错

你应该尝试做一次。不要尝试多次打开文件。并且在关闭之前不要打开同一个文件

【讨论】:

    【解决方案2】:

    你可以在你的代码周围去掉这部分,因为你没有使用你创建的 StreamReader:

        while ((line = read.ReadLine()) != null) 
        {
        ...
        }
    

    然后改变 File.WriteAllText("CurriculumVitae.txt", File.ReadAllText("CurriculumVitae.txt");

        File.WriteAllText("CurriculumVitae.txt", text);
    

    【讨论】:

      【解决方案3】:

      您需要更新 StreamReader 以在“共享”模式下打开文件,这样它就不会锁定文件。

      请参阅 this question 了解如何执行此操作的详细信息。

      【讨论】:

        【解决方案4】:

        首先,当你使用File.ReadAllText 时不要使用StreamReader,因为它不需要,另一个错误来自这一行:

        File.WriteAllText("CurriculumVitae.txt", File.ReadAllText("CurriculumVitae.txt").Replace("empty", DOB));
        

        你打开同一个文件两次,试试这样:

        string content = File.ReadAllText("CurriculumVitae.txt").Replace("empty", DOB);
        File.WriteAllText("CurriculumVitae.txt", content);
        

        【讨论】:

          【解决方案5】:

          使用StreamReaderReadAllText 但不能同时使用... 另外我真的建议尽可能做“使用”,因为这有助于很多关闭对象(但这不是你的主要问题)

          // It will free resources on its own.
          //
          using (StreamReader reader = new StreamReader("file.txt"))
          {
              line = reader.ReadLine();
          }
          Console.WriteLine(line);
          }
          

          【讨论】:

            猜你喜欢
            • 2018-03-27
            • 1970-01-01
            • 1970-01-01
            • 2013-03-15
            • 1970-01-01
            • 2011-07-05
            • 2015-12-07
            相关资源
            最近更新 更多