title: C# The process can't access the file because it is being used by another process (File Dialog)
body: <p>I have a rich text box that is being updated with log information. There is a button to save the log output to a file. When I use the code below to try to save the output to a file, I receive "The process can't access the file because it is being used by another process" exception.  I am not sure why I am receiving this exception.  It happens on new files that I create in the dialog.  It happens on any file I try to save the information to.    </p>
  
  <pre><code>private void saveLog_Click(object sender, EventArgs e)
  {
              OnFileDialogOpen(this, new EventArgs());
              // Displays a SaveFileDialog so the user can save the Image
              // assigned to Button2.
              SaveFileDialog saveFileDialog1 = new SaveFileDialog();
              saveFileDialog1.Filter = "Text File|*.txt|Log File|*.log";
              saveFileDialog1.Title = "Save Log File";
              saveFileDialog1.ShowDialog();
  
              // If the file name is not an empty string open it for saving.
              if (saveFileDialog1.FileName != "")
              {
                  // Saves the Image via a FileStream created by the OpenFile method.
                  System.IO.FileStream fs =
                     (System.IO.FileStream)saveFileDialog1.OpenFile();
                  // Saves the Image in the appropriate ImageFormat based upon the
                  // File type selected in the dialog box.
                  // NOTE that the FilterIndex property is one-based.
                  switch (saveFileDialog1.FilterIndex)
                  {
                      case 1:
  
                          try
                          {
                              this.logWindow.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show(ex.ToString());
                          }
  
  
                          break;
                      case 2:
  
                          try
                          {
                              this.logWindow.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show(ex.ToString());
                          }
  
                          break;
                  }
  
                  fs.Close();
                  OnFileDialogClose(this, new EventArgs());
              }
  }
  </code></pre>