簡體   English   中英

無法訪問文件,因為它正在被另一個進程使用

[英]File cannot be accessed because it is being used by another process

我對編碼有點陌生,我一直在嘗試替換文本文件中的單詞,但是當我執行程序時,它提示我“文件被另一個進程錯誤使用”

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;

}

首先,在使用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);

這三行的問題

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都將鎖定文件。 而且,每當他們嘗試鎖定同一文件時,都會出錯

您應該嘗試做一次。 不要嘗試多次打開文件。 並且不要在關閉之前打開同一文件

您可以僅在代碼中刪除此部分,因為您沒有使用所創建的StreamReader:

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

並更改File.WriteAllText(“ CurriculumVitae.txt”,File.ReadAllText(“ CurriculumVitae.txt”);

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

您將需要更新StreamReader以在“共享”模式下打開文件,以便它不會鎖定文件。

有關如何執行此操作的詳細信息,請參見此問題

請同時使用StreamReaderReadAllText但不要同時使用兩者...另外,我真的建議盡可能進行“用法”,因為這有助於關閉對象(但這不是您的主要問題)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM