#region Example 1
SaveFileDialog sfd = new SaveFileDialog ();
Set File type
SfD. Filter = "Backup file (*.bak) |*.bak";
Set default file type display order
SfD. FilterIndex = 1;
SfD. FileName = "Dbs_" + DateTime.Now.ToString ("yyyy_mm_dd_hh_mm_ss") + ". Bak";
Save dialog box remembers the last Open Directory
SfD. Restoredirectory = true;
Click the Save button to enter
if (SFD. ShowDialog () = = DialogResult.OK)
{
String Path= sfd. Filename.tostring (); Get file path
MessageBox.Show (PATH, "hint", MessageBoxButtons.OK, messageboxicon.warning);
}
Else
{
Return
}
#endregion
#region
Initialize OpenFileDialog
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
Set the initial path
Openfiledialog1.initialdirectory = "D:\\patch";
Selection criteria
Openfiledialog1.filter = "All Files (*. *) |*.*|txt files (*.txt) |*.txt";
property specifies that the default list box options are 1 by default
Openfiledialog1.filterindex = 1;
Openfiledialog1.restoredirectory = true;
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
Gets the resulting file path
Spath= Openfiledialog1.filename;
}
Else
{
Return
}
#endregion
Spath will get you the path to the selected file
Open File dialog box (OpenFileDialog)
1. The OpenFileDialog control has the following basic properties
InitialDirectory Initial directory of the dialog box
Filter the file filter to display in the dialog box, for example, "text file (*.txt) |*.txt| All Files (*. *) | | * * "FilterIndex the index of the file filter selected in the dialog box, set to 1 if the first item is selected restoredirectory the Control dialog box to restore the current directory before closing
FileName The first file to display in a dialog box or the last selected file
Title displays the characters in the title bar of the dialog box
AddExtension whether to add default extensions automatically
Checkpathexists Check if the specified path exists before the dialog box returns
DEFAULTEXT default name extension
Dereferencelinks whether to dereference a shortcut before returning from the dialog box
ShowHelp Enable the Help button
Validatenames the Control dialog box to check if the file name does not contain invalid characters or sequences
2. OpenFileDialog controls have the following common events
FileOk events to be handled when the user taps the "open" or "save" button
HelpRequest events to be handled when the user taps the Help button
You can use the following code to implement the above dialog box:
private void Openfiledialogbtn_click (object sender, System.EventArgs e) {OpenFileDialog openfiledialog=new OpenFileDialog ();
openfiledialog.initialdirectory= "c:\\";//Note that the path is written with c:\\ instead of C: \ openfiledialog.filter= "text file |*.*| C # files |*.cs| all Files |*.* ";
Openfiledialog.restoredirectory=true;
Openfiledialog.filterindex=1;
if (Openfiledialog.showdialog () ==dialogresult.ok)
{
Fname=openfiledialog.filename;
File Fileopen=new file (fName);
Isfilehavename=true;
Richtextbox1.text=fileopen.readfile ();
Richtextbox1.appendtext ("");
}
}
The return of the path is a string type with filename
such as: Openfiledialog1.showdialog ();
_name1= Openfiledialog1.filename;
Image imge = Image.FromFile (_name1);
3. Get the file name of the dialog box
OpenFileDialog. FileName//Gets or sets a filename string OpenFileDialog that is selected in the file dialog box. Safefilename//Gets the file name and extension in the selected dialog box
4. Example:
OpenFileDialog Open1 = new OpenFileDialog ();
Open1.filter = "Picture file (*.jpg,*.gif,*.bmp) |*.jpg|*.gif|*.bmp";
Description
| Split two, one is a comment, one is the real filter, and the comment is displayed. You should actually select JPG and BMP.
If you want to display more than one type of file at a time, separate it with semicolons
open1.filter= "picture file (*.jpg,*.gif,*.bmp) |*.jpg;*.gif;*.bmp";
Common uses of C # OpenFileDialog and SaveFileDialog