C#winForm程序開發,以對話窗口的形式選擇文件夾路徑。spa
/// <summary> /// 對話框形式選擇文件夾路徑 /// </summary> /// <returns>返回所選擇的文件夾路徑</returns> public string ChooseFolderPath() { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = System.Environment.SpecialFolder.Desktop; fbd.ShowNewFolderButton = true; fbd.Description = "請選擇目錄"; if (fbd.ShowDialog() == DialogResult.OK) { return fbd.SelectedPath.ToString(); } else { return ""; } }
/// <summary> /// 對話框形式選擇Excel文件路徑 /// </summary> /// <returns>返回所選擇的Excel文件路徑</returns> private string ChooseExcelFilePath() { OpenFileDialog opd = new OpenFileDialog(); opd.Filter = @"Excel文件 (*.xls,*.xlsx,*.xlsm) |*.xls;*.xlsx;*.xlsm"; opd.FilterIndex = 1; opd.RestoreDirectory = true; if (opd.ShowDialog() == DialogResult.OK) { return opd.FileName; } else { return ""; } }引用:
string path; if ((path =ChooseExcelFilePath())=="") { //do nothing } else { textBox1.Text = path; }