C# 選擇檔案 選擇路徑 儲存檔名稱路徑
阿新 • • 發佈:2020-11-17
{ class FilePathHelper { /// <summary> /// 選擇儲存檔案的名稱以及路徑 取消返回 空""; /// </summary> /// <param name="fileName"></param> /// <param name="filter"></param> /// <param name="title"></param> /// <returns></returns>public static string SaveFilePathName(string fileName=null, string filter=null, string title=null) { string path = ""; System.Windows.Forms.SaveFileDialog fbd = new System.Windows.Forms.SaveFileDialog(); if (!string.IsNullOrEmpty(fileName)) { fbd.FileName= fileName; } if (!string.IsNullOrEmpty(filter)) { fbd.Filter = filter;// "Excel|*.xls;*.xlsx;"; } if (!string.IsNullOrEmpty(title)) { fbd.Title = title;// "儲存為"; } if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path= fbd.FileName; } return path; } /// <summary> /// 選擇一個檔案 /// </summary> /// <param name="filter">如果需要篩選txt檔案("Files (*.txt)|*.txt")</param> /// <returns></returns> private static string SelectFile(string filter=null) { string path = string.Empty; var openFileDialog = new Microsoft.Win32.OpenFileDialog() { Filter = "Files (*.*)|*.*"//如果需要篩選txt檔案("Files (*.txt)|*.txt") }; if (filter!=null) { openFileDialog.Filter = filter; } var result = openFileDialog.ShowDialog(); if (result == true) { path = openFileDialog.FileName; } return path; } /// <summary> /// 選擇一個路徑 /// </summary> /// <returns></returns> public static string SelectPath() { string path = string.Empty; System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path = fbd.SelectedPath; } return path; } } }
轉載:https://blog.csdn.net/a376143220/article/details/79276562