2

I have been working on a clone of notepad and I have run into a problem. When I try to write the text in the textbox into a file which I create I get the exception:

The process cannot access the file 'C:\Users\opeyemi\Documents\b.txt' because it is being used by another process.

Below is the code I have written. I would really appreciate any advise on what I should do next.

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    SaveFileDialog TextFile = new SaveFileDialog();
    TextFile.ShowDialog();
  // this is the path of the file i wish to save
    string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),TextFile.FileName+".txt");
    if (!System.IO.File.Exists(path))
    {
        System.IO.File.Create(path);
        // i am trying to write the content of my textbox to the file i created
        System.IO.StreamWriter textWriter = new System.IO.StreamWriter(path);
        textWriter.Write(textEditor.Text);
        textWriter.Close();
    }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
opeyemi
  • 83
  • 1
  • 9
  • 2
    _"i would really appreciate any advise on what i should do next"_ - not opening a question without searching the web for the error you receive. A question about `File.Create()` which locks the file is asked about every two days here. – CodeCaster Aug 22 '13 at 10:49
  • possible duplicate of [File being used by another process after using File.Create()](http://stackoverflow.com/questions/2781357/file-being-used-by-another-process-after-using-file-create) – CodeCaster Aug 22 '13 at 10:50

1 Answers1

6

You must "protect" your StremWriter use (both read and write) in using, like:

using (System.IO.StreamWriter textWriter = new System.IO.StreamWriter(path))
{
    textWriter.Write(textEditor.Text);
}

no .Close() necessary.

You don't need the System.IO.File.Create(path);, because the StreamWriter will create the file for you (and the Create() returns a FileStream that you keep open in your code)

Technically you could:

File.WriteAllText(path, textEditor.Text);

this is all-in-one and does everything (open, write, close)

Or if you really want to use the StreamWriter and the File.Create:

using (System.IO.StreamWriter textWriter = new System.IO.StreamWriter(System.IO.File.Create(path)))
{
    textWriter.Write(textEditor.Text);
}

(there is a StreamWriter constructor that accepts FileStream)

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Thanks for the advise. It's working now but i would still like to know why when i used .close it didn't. It also didn't work when i tried the using statement. – opeyemi Aug 22 '13 at 11:04
  • @opeyemi This: ` System.IO.File.Create(path);` creates a file and keeps it open. Followed by this: `new System.IO.StreamWriter(path)` that creates a file and keep it open. You see the problem? Two instructions are trying to open the file. `File.Create` isn't "create a zero byte file and close it". It's "create a file and keep it open". It isn't `void Create(path)`, it's `FileStream Create(path)`. Technically you could have `File.Create(path).Close();` but it's useless. – xanatos Aug 22 '13 at 11:07