嘿,這裏有點麻煩。當窗體退出時關閉流式讀取和流式編寫器
所以我有一個加載按鈕,加載文件,並保存按鈕保存文件。我也有一個關閉程序的退出按鈕。我需要幫助的是關閉程序時,我不會檢查是否有任何StreamReader或StreamWriter沒有關閉的東西。
繼承人是我到目前爲止有: 在頂部,我declear這些傢伙
bool isDirtyBoolean = false;
string moreData = "";
我Load按鈕看起來像這樣
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;
}
}
我的保存按鈕看起來像這樣
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();
}
}
我的退出按鈕看起來像這樣
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isDirtyBoolean == false)
nameStreamReader.Close();
nameStreamWriter.Close();
this.Close();
}
我試圖做的是設置布爾isDirtyBoolean頂部,然後當流讀取器或寫入器關閉,將布爾值設置爲true,所以當我退出應用程序,如果它仍然設置爲false,無論如何它關閉它們。
但是這不工作,因爲isDirtyBoolean值是那些私人無效按鈕,我不能去他們。
我的任務它說退出按鈕需要再次檢查流是否關閉或不 – 2010-10-09 22:06:27