问题
Hey guys, having a bit of trouble here.
So I have a load button that loads the file, and a save button the saves the file. I also have a exit button that closes the program. What I need help with is when I close the program, I wont to check whether there are any StreamReader or StreamWriter things that have not been closed.
Heres what I have so far: At the top, i declear these guys
bool isDirtyBoolean = false;
string moreData = "";
My load button looks like this
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
//begin in the project folder
openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
//display the file open dialog box
DialogResult responseDialogResult;
responseDialogResult = openFileDialog1.ShowDialog();
if (responseDialogResult != DialogResult.Cancel)
{ //check that user did not click the cancel button
//create a streamreader object for the selected file,
//read the file contents,
//and display each line of the file in the list box
StreamReader nameStreamReader = new StreamReader(openFileDialog1.FileName);
while (nameStreamReader.Peek() != -1)
{
freindsDataListBox.Items.Add(nameStreamReader.ReadLine());
}
nameStreamReader.Close();
isDirtyBoolean = true;
}
}
And my save button looks like this
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
//begin in the project folder
saveFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
//display the file save dialog
DialogResult responseDialogResult;
responseDialogResult = saveFileDialog1.ShowDialog();
if (responseDialogResult != DialogResult.Cancel)
{ //check that user did not click the cancel button
//create a streamWriter object and
//then write out the list box items to the file
StreamWriter nameStreamWriter = new StreamWriter(saveFileDialog1.FileName);
int count = freindsDataListBox.Items.Count;
for (int i = 0; i < count; i++)
{
nameStreamWriter.WriteLine(freindsDataListBox.Items[i]);
}
nameStreamWriter.Close();
isDirtyBoolean = true;
freindsDataListBox.Items.Clear();
}
}
My exit button looks like this
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isDirtyBoolean == false)
nameStreamReader.Close();
nameStreamWriter.Close();
this.Close();
}
What I tried to do was set up the bool isDirtyBoolean up top, then when the Stream Reader or Writer closes, sets the bool value to true, so when i exit the app, if its still set to false, it closes them anyway.
But this doesnt work because the isDirtyBoolean value is those private void buttons and I cant get to them.
回答1:
To keep better track of things in general, close your readers and writers in the same method you open them in, rather than letting them live for the duration of the application.
If you use using
they will be disposed of (and closed) automatically. Examples:
Reader:
using (StreamReader reader = new StreamReader(openFileDialog1.FileName)) {
// do your stuff...
} // automatic close.
You can do the same with your writer instance.
Or if you're reading and writing at the same time you can open both and auto-close both at the end like so:
using (StreamReader reader = new StreamReader(openFileDialog1.FileName)) {
using (StreamWriter writer = new StreamWriter(path_to_other_file)) {
// now you're reading and writing
// DO YOUR STUFF
} // closes writer.
} // closes reader.
The reason using
works as it does is because of the IDisposable interface which is implemented by both the stream reader and writer classes; in fact any class implementing this interface is a candidate for using the using
statement on.
And remember to keep an eye out for caveats in the MSDN documentation like so:
StreamWriter.Dispose always closes stream
StreamWriter.Dispose always closes stream Disposing a StreamWriter closes the underlying stream when it is disposed, even if you use a constructor where you explicitly provide the stream. Sometimes you want to keep the stream open, but this class doesn't currently provide a mechanism to do that, except to not call Dispose, but be sure to call Flush instead.
回答2:
First of all, you are closing your streams alright - although I would use "using" statement to make sure they are closed even if there is an exception. So no need to close them when form exits.
Also this is bad coding:
if (isDirtyBoolean == false)
nameStreamReader.Close();
nameStreamWriter.Close();
Your if condition only works on the first statement. You need to put curly braces after if.
回答3:
Use "using" or write your own try/catch/finally blocks and close your streams in the finally.
I'm assuming this is homework so if your assignment requires you to check the streams you are going to have to declare your streams outside of the methods they are currently declared in. When you declare something in a method it only has scope in that method so the code that is currently in your exit button handler shouldn't even compile.
your missing brackets on your if in your exit button handler.
来源:https://stackoverflow.com/questions/3897996/closing-stream-read-and-stream-writer-when-the-form-exits