String resultfile = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog (); Openfiledialog1.initialdirectory = "D:\\patch"; Openfiledialog1.filter = "All Files (*. *) |*.*|txt files (*.txt) |*.txt"; Openfiledialog1.filterindex = 2; Openfiledialog1.restoredirectory = true; if (openfiledialog1.showdialog () = = DialogResult.OK) Resultfile = Openfiledialog1.filename;
Resultfile can get you the path of the selected file how to set the filter of the OpenFileDialog component, so that the implementation of filtering out a variety of file extensions at once??
Dlg. Filter = "Image Files (*. bmp;*. jpg;*. GIF) |*. bmp;*. jpg;*. Gif| All Files (*. *) |*.* "
Transferred from: http://www.cnblogs.com/HF06995000/archive/2011/2/22.html
Open File dialog box (OpenFileDialog)
1. The OpenFileDialog control has the following basic properties initialdirectory the initial directory of the dialog box filter to display the file filters in the dialog box, for example, "text file (*.txt) |*.txt| All Files (*. *) | | | * * "FilterIndex the index of the file filter selected in the dialog box, if the first item is selected, set to 1 restoredirectory whether the Control dialog box restores the current directory before closing the first file or the last selected file that appears in the dialog box Title Displays the character AddExtension in the title bar of the dialog box whether the default extension is automatically added checkpathexists before the dialog returns, check that the specified path exists DefaultExt default extension dereferencelinks Whether to dereference the shortcut before returning from the dialog ShowHelp enable the Help button Validatenames Control dialog box to check if the file name does not contain an invalid character or sequence
2. OpenFileDialog controls have the following common events FileOk events to be handled when the user taps the "open" or "save" button helprequest the event to be handled when the user taps the Help button
You can use the following code to implement this 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
| 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 open1.filter= "picture file (*.jpg,*.gif,*.bmp) |*.jpg;*.gif;*.bmp" with semicolons;
C#openfiledialog Use (Turn)