Using the SaveFileDialog Control

The SaveFileDialog control has the same properties as the OpenFiledialog does. In fact , to show you how you can utilize both, I have mode the code to use the SaveFileDialog a little more complicated;

string strFileName = openFileDialog1.FileName;
if(strFileName != "")
{
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
strFileName= saveFileDialog1.FileName;
richTexBox1.SaveFile(strFileName);
}
}
else
{
richTexBox1.SaveFile(strFileName);
}

This code tests to see if the openFileDialog1 control’s FileName property has been set , through openin a file. If not and the FileName property is equal the name of the dialog box, which is the default, then the SaveFileDialog control is displayed , and the user can specify the file to Save As. If a file has been specified in the openFileDialog then that name is used This is show to Save Option for a new file generally behaves, by only asking for the filename if  it hasn’t been save before.

Leave a Reply