Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, May 1, 2013 7:36 PM
Hi All,
In my application, I have one class (first) calling a function in another class (second). This first class needs to use an output text file generated (via StreamWriter) in the second class (read it and use it via StreamReader in the first class). In debug mode, the following exception hits at the instance of StreamWriter (in the second class):
The process cannot access the file because it is being used by another process
What might be causing this (from the first class)? I assume it has to do with the StreamReader in that first class. Is there a "pause" or "wait" command that could rectify this problem?
Appreciate any advice. Thanks.
-AD-
AndrewDen
All replies (6)
Wednesday, May 1, 2013 8:43 PM âś…Answered
try changing the read logic, without locking the file. Better solution would be using these stream objects inside using blocks so that they will be closed / disposed once after you are done with the objects.
using(FileStream stream = new FileStream(@"C:\temp\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
using(StreamReader reader = new StreamReader(stream)){
string str = reader.ReadToEnd();
//reader.Close();}}
--Krishna
Wednesday, May 1, 2013 7:44 PM
you need to organize the way how the streams will be shared across read and write.
Below sample creates your error scenario. (If you call Close() on writer then read will succeed. Hope this will help you to understand your issue.
StreamWriter writer = new StreamWriter(@"C:\temp\test.txt");
writer.WriteLine("Line 1");
writer.WriteLine("Line 2");
//writer.Close(); // comment line will cause the reader exception.
StreamReader reader = new StreamReader(@"C:\temp\test.txt");
string str = reader.ReadToEnd();
--Krishna
Wednesday, May 1, 2013 8:08 PM
I understand that part, but it's stopping and throwing an error on the instance of StreamWriter.
I have .Close() statements written later on, after each file is written.
The format looks like this:
Class 1:
GUI.CSVtoText.Go(date: date, hr: hr); //Calls CSVtoText function (in Class 2)
//Shouldn't it wait here until the "POTT_" + date + "_" + hr + ".txt" is written in the CSVtoText function?
//The following statement (which may be causing the exception)
StreamReader read = new StreamReader("POTT_" + date + "_" + hr + ".txt");
Class 2:
//Throws exception here, on this statement
//"The process cannot access the file 'POTT_" + date + "_" + hr + ".txt' because it is being used by another process"
StreamWriter P_w = new StreamWriter("POTT_" + date + "_" + hr + ".txt");
Solutions?
AndrewDen
Monday, May 6, 2013 5:24 PM
Does this just open and read the file for the time that it's needed then?
Without the "using" statement, does it lock up StreamReader and StreamWriter? Is that what could be causing the problem?
-AD-
AndrewDen
Monday, May 6, 2013 5:46 PM | 1 vote
The answer that Krishnav gave above was correct. The notion of pausing or waiting on the StreamReader is not correct. It doesn't matter whether the StreamWriter/Reader is currently, at that very second, reading or writing from the file: when you create a StreamReader or StreamWriter, the file that the Writer/Reader is pointed is marked as in use until you close the reader/writer (StreamReader.Close(), StreamWriter.Close()).
You cannot open a StreamWriter in Class 2 pointing to POTT_" + date + "_" + hr + ".txt because you have an open StreamReader in Class 1 open on the same file.
Monday, May 6, 2013 5:56 PM
Thank you very much. That makes sense. I'm fairly new to C# code, so I'm still learning all of the nuances.
AndrewDen