How to add Save file Dialog Box using C#

Shamim Hafiz picture Shamim Hafiz · Sep 14, 2011 · Viewed 19k times · Source

I need to implement something similar to Notepads' save option. Assuming I have a button placed next to a RichTextBox, what I want is, when this button is clicked, a Dialogue box will open up, which will look similar to the one that appears when Save As is clicked. I would like to save the content of the RichTextBox in text format, by entering the name of file in the Save Dialogue box.

Answer

Overlock picture Overlock · Sep 14, 2011
private void Save_As_Click(object sender, EventArgs e)
{
  SaveFileDialog _SD = new SaveFileDialog(); 
  _SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
  _SD.FileName = "Untitled"; 
  _SD.Title = "Save As";
  if (__SD.ShowDialog() == DialogResult.OK)
  {
   RTBox1.SaveFile(__SD.FileName, RichTextBoxStreamType.UnicodePlainText);
  }
}