Cannot open file to write
-
I did:
StreamReader sr = new StreamReader(path);
sr.ReadToEnd();
sr.Close();StreamWriter sw = new StreamWriter(path);
I got error "The process cannot access the file because it is being used by another process" Why this happen, I already closed the file When in debug mode, it sometimes successfully openned the file, sometimes failed
-
I did:
StreamReader sr = new StreamReader(path);
sr.ReadToEnd();
sr.Close();StreamWriter sw = new StreamWriter(path);
I got error "The process cannot access the file because it is being used by another process" Why this happen, I already closed the file When in debug mode, it sometimes successfully openned the file, sometimes failed
Try opening the file as a filestream, and then passing the filestream handle to the streamreader and writer.
-
Try opening the file as a filestream, and then passing the filestream handle to the streamreader and writer.
-
I changed it to
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
sr.ReadToEnd();
sr.Close();StreamWriter sw = new StreamWriter(fs);
and still doesn't work I did wrong somewhere?
all I can think of is maybe making sr = null; after you close it. I can't check right now though so...
-
I changed it to
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
sr.ReadToEnd();
sr.Close();StreamWriter sw = new StreamWriter(fs);
and still doesn't work I did wrong somewhere?
FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs); StreamWriter sw = new StreamWriter(fs); sr.ReadToEnd(); sr.Close(); sw.Close(); fs.Close();
Closing the streamreader before initializing the streamwriter causes a Stream was not writable. error. Keep the reader and writer open until you close the file.