title: Closing Stream Read and Stream Writer when the form exits
body: <p>Hey guys, having a bit of trouble here.</p>
  
  <p>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.</p>
  
  <p>Heres what I have so far:
  At the top, i declear these guys</p>
  
  <pre><code>    bool isDirtyBoolean = false;
      string moreData = "";
  </code></pre>
  
  <p>My load button looks like this</p>
  
  <pre><code>    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;
          }
      }
  </code></pre>
  
  <p>And my save button looks like this</p>
  
  <pre><code>        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 &lt; count; i++)
              {
                  nameStreamWriter.WriteLine(freindsDataListBox.Items[i]);
              }
  
              nameStreamWriter.Close();
              isDirtyBoolean = true;
              freindsDataListBox.Items.Clear();
          }
      }
  </code></pre>
  
  <p>My exit button looks like this</p>
  
  <pre><code>        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
          if (isDirtyBoolean == false)
              nameStreamReader.Close();
              nameStreamWriter.Close();
  
          this.Close();
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>But this doesnt work because the isDirtyBoolean value is those private void buttons and I cant get to them.</p>