OpenFileDialogIt is a commonly used dialog box control in C#, which allows users to select files. Here are some of the common attributes:
- Title: Set the title text of the dialog box.
- Multiselect: Set whether to allow multiple files to be selected, the default value is false. If set to true, you can hold down the Ctrl or Shift keys and select multiple files at the same time.
- Filter: Sets the file type that can be selected, the format is "Descriptive Text|File Type", and multiple types are separated by vertical lines |. If not set, all files can be selected by default. Here is an example:
= "Text Files|*.txt|Word Documents|*.doc|All Files|*.*";
In addition to the properties mentioned above, OpenFileDialog also has some commonly used methods, and the following are some of them:
- ShowDialog(): Displays the dialog box for opening the file and returns the dialog results. If the user selects a file, the dialog box result is true; if the user deselects, the dialog box result is false.
- FileNames: Gets the file name selected by the user. If the Multiselect property is true, it returns a string array; otherwise, it returns a string array of length 1.
- FileName: Get the first file name selected by the user, which is equivalent to FileNames[0].
Sample code:
OpenFileDialog openFileDialog1 = new OpenFileDialog(); = "Select a file"; = true; = "Text Files|*.txt|All Files|*.*"; if (() == ) { foreach (string fileName in ) { ("Selected File:" + fileName); } }
This code uses the OpenFileDialog dialog box to select one or more files.
- First create an OpenFileDialog object openFileDialog1.
- Sets the title of the dialog box, whether to allow multiple selections, file type filter and other properties.
- Call the ShowDialog() method to open the dialog box and wait for the user to operate. If the user presses the OK button, execute the following code.
- In case of selecting a file, iterate over the FileNames property, which contains the full path to all files selected by the user.
foreach (string fileName in ) { ("Selected File:" + fileName); }
- In the above example code, the Multiselect property is set to true, so the user can select multiple files. When traversing the FileNames property, we can get the full path to each file and output it to the console.
public static List<string> GetOpenFilesPath(string title, bool canBeMultiple = false, string filter = "") { List<string> list = new List<string>(); OpenFileDialog dialog = new OpenFileDialog { Title = title, Multiselect = canBeMultiple, Filter = (filter) ? "All files(*.*)|*.*" : filter }; if (().Value) { (); } return list; }
The purpose of this code is to open the file dialog box, let the user select one or more files, and then return to the selected file path list.
- title: The title of the file dialog box, usually a string, is used to prompt the user for the purpose of the file currently selected
- canBeMultiple: A boolean value indicating whether multiple files are allowed to be selected. If the value is true, the file dialog box will display the "Multiple Select" button, and the user can select multiple files at the same time; otherwise, the file dialog box will only display the "Open" button, and the user can only select one file.
- filter: A string representing the file type that can be displayed in the file dialog box. The format of this string is usually "Type Name (.Extension) |.Extension" (multiple types are separated by vertical symbols |), such as "Text File (.txt) |.txt|All Files (.) |." If the parameter is an empty string or null, all file types are displayed by default.
The return value of this method is a list of strings, indicating the full path to the file selected by the user. If the user does not select any files, an empty list is returned.
For example, suppose you want the user to select one or more image files and only display the .png and .jpg file types, then you can execute the following code:
List<string> imagePathList = GetOpenFilesPath("Select picture file", true, "PNG document(*.png)|*.png|JPG document(*.jpg)|*.jpg");
A file dialog box will open with the title "Select Picture File", which allows you to select multiple files, showing only the .png and .jpg file types. The file path selected by the user will be saved in the imagePathList list, which you can traverse, process and other operations on.
This is the article about the use of the C# OpenFileDialog dialog control. For more related contents of the C# OpenFileDialog dialog control, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!