0

I'm kind of new to coding and I've been trying to replace a word in a text file but when I execute the program it gives me the "File is used by another process error"

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;

}
Johan
  • 8,068
  • 1
  • 33
  • 46
MTHO77
  • 1
  • Oh please ignore the text under my question that was an innocent mistake – MTHO77 Aug 12 '15 at 15:58
  • Why are you trying to read line by line but *then* `ReadAllText`? That does not make any sense. You also read the file again do the replace *again*. – crashmstr Aug 12 '15 at 16:03

5 Answers5

0

First, don't use a StreamReader when you use File.ReadAllText as it's not needed, the other error comes from this line:

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

You are opening the same file twice, try something like this:

string content = File.ReadAllText("CurriculumVitae.txt").Replace("empty", DOB);
File.WriteAllText("CurriculumVitae.txt", content);
w.b
  • 11,026
  • 5
  • 30
  • 49
0

Problem from these 3 lines

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

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

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

Both StreamReader and File.ReadAllText will lock a file. And whenever they try to lock same file it will error

You should try to do thing once. Don't try to open file many times. And don't open same file before it closed

Thaina Yu
  • 1,372
  • 2
  • 16
  • 27
0

You can just take out this part around your code, as you're not using the StreamReader you created:

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

And change File.WriteAllText("CurriculumVitae.txt", File.ReadAllText("CurriculumVitae.txt");

To

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

You will want to update your StreamReader to open the file in "shared" mode so that it doesn't lock the file.

See this question for details on how to do that.

Community
  • 1
  • 1
Dillie-O
  • 29,277
  • 14
  • 101
  • 140
0

Use either StreamReader or ReadAllText but not both at the same time... Also I would really suggest to do "usings" wherever possible becuase this helps a lot closing objects (but is not your prime problem here)

// It will free resources on its own.
//
using (StreamReader reader = new StreamReader("file.txt"))
{
    line = reader.ReadLine();
}
Console.WriteLine(line);
}
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41