C # use an instance in the OpenFileDialog file dialog box,
The following are the attributes of the OpenFileDialog class!
// Obtain or set a value indicating that if the extension is omitted,
Whether to automatically add an extension to the file name in the dialog box.
Dlg. AddExtension = cbAddExtension. Checked;
// Obtain or set a value that indicates that if the user specifies a file name that does not exist
Whether a warning is displayed in the dialog box
Dlg. CheckFileExists = cbCheckFileExists. Checked;
// Obtain or set a value indicating that if the user specifies a path that does not exist,
Whether a warning is displayed in the dialog box
Dlg. CheckPathExists = cbCkeckPathExists. Checked;
// Obtain or set a value indicating whether multiple files can be selected in the dialog box
Dlg. Multiselect = cbMltiSelect. Checked;
// Obtain or set a value indicating whether to select the read-only check box
Dlg. ReadOnlyChecked = cbReadOnlyChecked. Checked;
// Obtain or set a value indicating whether to restore the selected directory before closing the dialog box
Dlg. RestoreDirectory = cbRestoreDirectory. Checked;
// Indicates whether the Help button is displayed in the file dialog box.
Dlg. ShowHelp = cbShowHelp. Checked;
//
Dlg. ShowReadOnly = cbShowReadOnly. Checked;
// Indicates whether the dialog box only accepts valid win32 file names.
Dlg. ValidateNames = cbValidateNames. Checked;
// Get or set the default file extension
Dlg. DefaultExt = txtDefaultExt. Text;
// Obtain or set a string containing the selected file name in the file dialog box
Dlg. FileName = txtFileNames. Text;
// Obtain or set the filter string for the current file name. This string determines the selected content in the "Save As file type" or "file type" dialog box.
Dlg. Filter = txtFilter. Text;
If (txtFilterIndex. Text. Trim (). Length <1)
Dlg. FilterIndex = 1;
Else
Dlg. FilterIndex = int. Parse (txtFilterIndex. Text );
Dlg. InitialDirectory = txtInirialDirecory. Text;
Dlg. Title = txtTitle. Text;
If (dlg. ShowDialog () = DialogResult. OK)
{
TxtFileName. Text = dlg. FileName;
TxtFileNames. Lines = dlg. FileNames;
}
Change the title of the main form to "test file dialog box", and add the attachment shown in the image to the main form.
The code in the Form1.cs file is as follows:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace FileTest {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnOpenFileDialog_Click (object sender, EventArgs e) {// create dialog box instance OpenFileDialog dlg = new OpenFileDialog (); // set the properties of the dialog box dlg. addExtension = cbAddExtension. checked; dlg. checkFileExists = cbCheckFileExists. checked; dlg. checkPathExists = cbCkeckPathExists. checked; dlg. multiselect = cbMltiSelect. checked; dlg. readOnlyChecked = cbReadOnlyChecked. checked; dlg. restoreDirectory = cbRestoreDirectory. checked; dlg. showHelp = cbShowHelp. checked; dlg. showReadOnly = cbShowReadOnly. checked; dlg. validateNames = cbValidateNames. checked; dlg. defaultExt = txtDefaultExt. text; dlg. fileName = txtFileNames. text; dlg. filter = txtFilter. text; if (txtFilterIndex. text. trim ()). length <1) dlg. filterIndex = 1; else dlg. filterIndex = int. parse (txtFilterIndex. text); dlg. initialDirectory = txtInirialDirecory. text; dlg. title = txtTitle. text; if (dlg. showDialog () = DialogResult. OK) {txtFileName. text = dlg. fileName; txtFileNames. lines = dlg. fileNames ;}} private void btnSaveFileDialog_Click (object sender, EventArgs e) {SaveFileDialog dlg = new SaveFileDialog (); dlg. addExtension = cbAddExtension. checked; dlg. checkFileExists = cbCheckFileExists. checked; dlg. checkPathExists = cbCkeckPathExists. checked; dlg. restoreDirectory = cbRestoreDirectory. checked; dlg. showHelp = cbShowHelp. checked; dlg. validateNames = cbValidateNames. checked; dlg. createPrompt = cbCreatPrompt. checked; dlg. overwritePrompt = cbOverwritePrompt. checked; dlg. defaultExt = txtDefaultExt. text; dlg. fileName = txtFileNames. text; dlg. filter = txtFilter. text; if (txtFilterIndex. text. trim ()). length <1) dlg. filterIndex = 1; else dlg. filterIndex = int. parse (txtFilterIndex. text); dlg. initialDirectory = txtInirialDirecory. text; dlg. title = txtTitle. text; if (dlg. showDialog () = DialogResult. OK) txtFileName. text = dlg. fileName ;}}}