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.