I get this error: the process cannot access the file (...) because it is being used by another process. I tried using 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'm trying to do is access the file, write on it, and then close it. I could make a stupid mistake, but I would like to understand what I am doing wrong and why. How to make sure that the file is closed and does not cause this error.
Answers helped 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(); }
This seems to be better because it seems to close / stop the process of my file if I try to access another file the second time I access the page. But if I try to access the same file a second time, it will again give me an error.
source share