title: Writing a TEXT file using C#
body: <p>I am trying to add a SaveFileDialog to my Windows Application, in which I save a simple text file from the contents of a multi-line text box.  The program seems to work fine up until the save dialog, and I don't get any errors, but once I click the save button the application just hangs.  The only way out of it is to click the "stop-debugging" button.  Here are the code sections that I have tried, but both create the same hang:</p>
  
  <pre><code>private void button_SaveToFile_Click(object sender, EventArgs e)
      {
          SaveFileDialog saveFileDialog1 = new SaveFileDialog();
          saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";            
          saveFileDialog1.RestoreDirectory = true;
  
          if (saveFileDialog1.ShowDialog() == DialogResult.OK)
          {
                 File.WriteAllText(saveFileDialog1.FileName, textBox_ListDestination.Text);
          }            
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>private void button_SaveToFile_Click(object sender, EventArgs e)
      {
          Stream myStream;
          SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  
          saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
          saveFileDialog1.FilterIndex = 2;
          saveFileDialog1.RestoreDirectory = true;
  
          if (saveFileDialog1.ShowDialog() == DialogResult.OK)
          {
              if ((myStream = saveFileDialog1.OpenFile()) != null)
              {
                  File.WriteAllText(saveFileDialog1.FileName, textBox_ListDestination.Text);
                  myStream.Close();
              }
          }
      }
  </code></pre>