How to use savefiledialog box in the c#.net ?

// Create a new SaveFileDialog object
SaveFileDialog obDialogSave = new SaveFileDialog();

// Set Default file extension to dialog
obDialogSave.DefaultExt = "txt";

// Other Available file extensions to dialog
obDialogSave.Filter = "Text file (*.txt)|*.txt|XML file (*.xml)|*.xml|All files (*.*)|*.*";

// Adds a extension if the user does not select
obDialogSave.AddExtension = true;

// Restores the selected directory, next time
obDialogSave.RestoreDirectory = true;

// To display Dialog title
obDialogSave.Title = "Where do you want to save the file?";

// Start directory
obDialogSave.InitialDirectory = @"C:/";

// Show the dialog and process the result
if (obDialogSave.ShowDialog() == DialogResult.OK)
{
// your code go here if user click on “OK” button.
MessageBox.Show("You selected the file: " + DialogSave.FileName);
}
else {
// your code go here if user click on “Cancel” button.
MessageBox.Show("You click cancel.");
}

obDialogSave.Dispose();

// Set object to null.
obDialogSave = null;

2 comments:

  1. Interesting ... :)

    ReplyDelete
  2. Not this,How to save the content of a textbox to text file,using a savefile dialog

    ReplyDelete

Thank you very much