I'm writing a program that uses text files in C#.
I use a parser class as an interface between the file structure and the program.
This class contains a StreamReader
, a StreamWriter
and a FileStream
. I use the FileStream
as a common stream for the reader and the writer, else these two will conflict when both of them have the file open.
The parser class has a class variable called m_path
, this is the path to the file. I've checked it extensively, and the path is correct. OpenStreams()
and and ResetStreams()
work perfectly, however after calling CloseStreams()
in the delete()
function, the program goes to the catch clause, so File.Delete(m_path)
won't get executed. In other situations the CloseStreams()
function works perfectly. It goes wrong when I'm trying to close the StreamReader (m_writer)
, but it does give an exception (File is Already Closed).
/**
* Function to close the streams.
*/
private void closeStreams() {
if (m_streamOpen) {
m_fs.Close();
m_reader.Close();
m_writer.Close(); // Goes wrong
m_streamOpen = false;
}
}
/**
* Deletes the file.
*/
public int delete() {
try {
closeStreams(); // Catch after this
File.Delete(m_path);
return 0;
}
catch { return -1; }
}
I call the function like this:
parser.delete();
Could anybody give me some tips?