C# select file and select directory

1 Select file ( OpenFileDialog )

OpenFileDialog is a class that can be instantiated to set a file dialog box to pop up, such as selecting the log file to be parsed, uploading an EXCEL file, uploading a picture, etc.

common attributes
Attributes type illustrate
Title string Set popup title
InitialDirectory string set absolute path
Filter string set file type
Multiselect bool Whether to set multiple selection
FileName string Single choice returns the selected file name
safefileName string[] Array of multi-select filenames

example

OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "所有文件(*.*)|*.*";
dialog.InitialDirectory = @"E:\";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 {
     string fullFileName = dialog.FileName;// @"e:\日记.doc";
     var FileName =System.IO.Path.GetFileName(fullFileName); //文件名  “日记.doc”
     string extension = System.IO.Path.GetExtension(fullFileName);//扩展名 “.doc”
     string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullFileName);// 没有扩展名的文件名 “日记”

 }

2 Select folder ( System.Windows.Forms.FolderBrowserDialog )

Windows Forms  FolderBrowserDialog FolderBrowserDialog The FolderBrowserDialog  component is a modal dialog box for browsing and selecting folders.  It is also possible to create a new folder through  the FolderBrowserDialog FolderBrowserDialog FolderBrowserDialog component.

example

FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "选择匹配目录"; ;//左上角提示
string path = string.Empty;
if (dialog.ShowDialog() == DialogResult.OK)
{
   path = dialog.SelectedPath;//获取选中文件路径
}

Guess you like

Origin blog.csdn.net/qq_29728817/article/details/128146314