1 选择文件(OpenFileDialog)
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 选择文件夹(System.Windows.Forms.FolderBrowserDialog)
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog(); dialog.Description = "请选择Txt所在文件夹"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = dialog.SelectedPath; // "e:/go" }