2010-10-09 63 views
1

嘿,这里有点麻烦。当窗体退出时关闭流式读取和流式编写器

所以我有一个加载按钮,加载文件,并保存按钮保存文件。我也有一个关闭程序的退出按钮。我需要帮助的是关闭程序时,我不会检查是否有任何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值是那些私人无效按钮,我不能去他们。

回答

1

首先,你正在关闭你的流 - 虽然我会使用“使用”语句,以确保它们是封闭的,即使有异常。因此,在表单退出时无需关闭它们。

而且这是坏的编码:

if (isDirtyBoolean == false) 
     nameStreamReader.Close(); 
     nameStreamWriter.Close(); 

你如果条件只适用于第一个声明。如果之后需要放大括号。

+0

我的任务它说退出按钮需要再次检查流是否关闭或不 – 2010-10-09 22:06:27

1

使用“使用”或编写自己的try/catch/finally块并在finally中关闭流。

我假设这是家庭作业,所以如果你的任务需要你检查流,你将不得不声明你的流在他们当前声明的方法之外。当你在方法中声明某些东西时,它只有该方法的范围,所以当前在你的退出按钮处理程序中的代码甚至不应该编译。

如果您在退出按钮处理程序中丢失了括号。

2

为了更好地跟踪事物的总体情况,请使用与您打开它们相同的方法关闭您的读者和作者,而不是让他们在应用程序期间生活。

如果您使用using,它们将自动处理(并关闭)。例子:

读者:

using (StreamReader reader = new StreamReader(openFileDialog1.FileName)) { 

    // do your stuff... 

} // automatic close. 

你可以做同样的刻录机的实例。

或者,如果你正在阅读和写作的同时,你都可以打开并自动关闭无论是在年底,像这样:

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. 

的原因,因为它确实using作品是因为IDisposable的由流读取器和写入器类实现的接口;实际上任何实现这个接口的类都是使用using声明的候选者。


而且记得要留意的注意事项MSDN文档中像这样:

StreamWriter.Dispose always closes stream

StreamWriter.Dispose始终关闭 流处置一个StreamWriter关闭 底层即使您使用的是 构造函数,您在其中明确指定了 提供流。有时你想 保持流打开,但这个 类目前没有提供一个 机制来做到这一点,除非不呼吁 调用Dispose,但一定要调用 Flush代替。

相关问题