5

I am getting this error: The process cannot access the file (...) because it is being used by another process. I have tried to use File.WriteAllText;

StreamWriter sw = new StreamWriter(myfilepath);
                sw.Write(mystring);
                sw.Close();
                sw.Dispose();

;

using (FileStream fstr = File.Create(myfilepath))
                {
                StreamWriter sw = new StreamWriter(myfilepath);
                sw.Write(mystring);
                sw.Close();
                sw.Dispose();
                fstr.Close();
                }

All I am trying to do is to access a file, write on it, then close it. I might be making a silly mistake but I would like to understand what I am doing wrong and why. How to make sure that the file is closed and not to cause this error again.

Helped by the answers so far I did this:

using (FileStream fstr = File.Open(myfilepath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
                {
                    StreamWriter sw = new StreamWriter(fstr);
                    sw.Write(mystring);
                    sw.Close();


                }

It seems to be better because it seems to close/stop the process of my file if I try to access another file on the second time I access the page. But if I try to access the same file on a second time, it gives me the error again.

Jenninha
  • 1,357
  • 2
  • 20
  • 42
  • and what happens with the first or second code snippet? if you use the first snippet with a using on the StreamWriter it should work and no need for you to call close or dispose. see here: http://stackoverflow.com/questions/7710661/do-you-need-to-call-flush-on-a-stream-if-you-are-using-using-statement/7710686#7710686 – Davide Piras Jul 27 '12 at 11:41
  • 1
    Your second approach is somewhat strange. You're creating a FileStream but you're never using it(`StreamWriter sw = new StreamWriter(fstr);`). You also don't need to dispose it since you're using the `using-statement` anyway. – Tim Schmelter Jul 27 '12 at 11:44

4 Answers4

4

Why not just use:

System.IO.File.WriteAllText(myfilepath, mystring");

That should not lock your file.

Internally WriteAllText uses FileShare.Read and releases that lock as soon as it is done writing.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
2

"because it is being used by another process" that's the clue. Do you by chance have the file open in Notepad or something?

You may need to set the sharing mode when you open the file to allow readers, and ask only for the permission you need (write access).

Ben
  • 34,935
  • 6
  • 74
  • 113
1

I would like to thank everyone for the help. In fact, apart from this code I found out that I had a stremReader still opened somewhere else after the code above. At the end I changed the code I had before for this:

using (FileStream fstr = File.Open(myfile, FileMode.OpenOrCreate, FileAccess.ReadWrite))

                    {
                        StreamWriter sw = new StreamWriter(fstr);
                        sw.Write(mystring);
                        sw.Flush();
                        sw.Dispose();
                    }

and on my StreamReader I did this:

StreamReader sr = new StreamReader(myfile);
string sometext = sr.ReadToEnd();
sr.Dispose();

I could also use this:

File.ReadAllText(myfile);

If there is something that I could have done in a better way please tell me. Thank you very much.

Jenninha
  • 1,357
  • 2
  • 20
  • 42
0

Try this

FileStream fs = new FileStream(myfilepath, FileMode.Create, FileAccess.Write);            
byte[] bt = System.Text.Encoding.ASCII.GetBytes(mystring);
fs.Write(bt, 0, bt.Length);
fs.Close();
Renju Vinod
  • 254
  • 1
  • 3
  • 11