C# opens the Select File dialog box and the Select Directory dialog box
1.Select File Dialog
First reference the using Microsoft.Win32 namespace.
private void BrowseButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if((this)== true) { = ; } }
2. Select the directory dialog box and use the winForm library
Reference using; If this namespace cannot be found, right-click the project - Add - Reference - to find and add it in the assembly.
private void DisplayButton_Click(object sender, RoutedEventArgs e) { try { FolderBrowserDialog dialog = new FolderBrowserDialog(); = "Please select the file path"; DialogResult result = (); if(result == ) { return; } string folderPath = (); DirectoryInfo theFolder = new DirectoryInfo(folderPath); if () { //operate return; }
C# Universal Select File Dialog
Universal Select File Dialog Encapsulation
/// <summary> /// File selection universal dialog box /// </summary> public class FilesSelectDialog { public OpenFileDialog fileDialog; //The path is opened by default public string DirPath = "D:\\"; public string FilePath; public string Title; public string Filter= "picture(*.jpg,*.jpge,*.bmp,*.png)|*.jpg;*.jpge;*.bmp;*.png| All files(*.*) |*.*"; public FilesSelectDialog(string title) { Title = title; fileDialog = new OpenFileDialog();//Open the file dialog } public bool Show() { = DirPath;//Initialize the path = Filter;//Filter option settings, text files, all files. = 0;//The second filter string is currently used = true;//Restore the original directory when the dialog box is closed = Title; if (() == ) { //for (int i = 1; i <= ; i++) //{ // if (( - i, 1).Equals(@"\")) // { // //Change the default path to the most recent open path // Path = (0, - i + 1); // return true; // } //} FilePath = ; try { DirPath = (FilePath);//Change the default path to the most recent open path } catch (Exception) { return false; } return true; } else { return false; } } } FilesSelectDialog selectDialog = new FilesSelectDialog("Please select the picture you want to convert"); private void Btn_OpenImsge_Click(object sender, EventArgs e) { //FilesSelectDialog selectDialog = new FilesSelectDialog("Please select the picture to be converted"); = "picture(*.jpg,*.jpge,*.bmp,*.png)|*.jpg;*.jpge;*.bmp;*.png| All files(*.*) |*.*"; if (()==true) { = ; //(); DisplayImage(); } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.