string resultFile = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = @"D:\Patch";
dlg.Filter = "所有檔案 (*.*)|*.*|文字檔 (*.txt)|*.txt";
dlg.FilterIndex = 2; // 預設選「文字檔」
dlg.RestoreDirectory = true; // 關閉前還原當前目錄
if (dlg.ShowDialog() == DialogResult.OK)
resultFile = dlg.FileName;
顯示文字|檔案模式|顯示文字|檔案模式... — 以 | 分隔,成對出現。FilterIndex 從 1 起算。
string resultFolder = "";
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.RootFolder = Environment.SpecialFolder.MyComputer; // 從「我的電腦」開始
if (dlg.ShowDialog() == DialogResult.OK)
resultFolder = dlg.SelectedPath;
| 列舉值 | 對應路徑 |
|---|---|
| MyComputer | 我的電腦 (根節點) |
| Desktop | 桌面 |
| MyDocuments | 我的文件 |
| ProgramFiles | Program Files |
| System | Windows\System32 |
IDisposable,建議包在 using 區塊自動釋放資源。InitialDirectory 若路徑不存在,對話方塊會回退至預設位置 (通常為 Documents)。RestoreDirectory = true 可避免改變應用程式當前工作目錄,影響後續相對路徑操作。