# C# OpenFileDialog and SaveFileDialog OpenFileDialog 和 SaveFileDialog其實語法上都差不多,可以看最後的兩者差異介紹 這是一個可以讓使用者自己選擇檔案開啟 結合檔案的書寫更好用:[C# File](/OlPtFGQTTeCy5eu-ZggsGw) >ChatGPT提供 [TOC] ## 主控制台所需 ```csharp= class Program { [STAThread] // 需要這個標記來運行 UI 元件 } ``` ## 宣告 ofd就為OpenFileDialog的替身 ```csharp= OpenFileDialog ofd = new OpenFileDialog(); ``` ## Title 標題 Title為設定對話框標題 ```csharp= ofd.Title = "選擇一個檔案"; ``` ## Filter 檔案類型 Filter為設定可選擇的檔案類型,例如:圖片檔案 (.jpg;.png) ```csharp= ofd.Filter = "所有檔案 (*.*)|*.*|文字檔 (*.txt)|*.txt"; // 設定可選擇的檔案類型 ``` 檔案類型: | 檔案類型 | 設定方式 | | ---------------- | -------------------------------- | | 所有檔案 | `"所有檔案 ( . )` | | 文字檔 | `"文字檔案 (*.txt)` | | CSV 檔 | `"CSV 檔案 (*.csv)` | | 圖片檔 | `"圖片檔案 ( .jpg; .png;*.gif)` | | 音樂檔 | `"音樂檔案 ( .mp3; .wav;*.flac)`| | Word 文件 | `"Word 文件 ( .doc; .docx)` | | Excel 文件 | `"Excel 檔案 ( .xls; .xlsx)` | | PDF 檔案 | `"PDF 檔案 (*.pdf)` | | C# 程式碼檔案 | `"C# 檔案 (*.cs)` | ## 開啟多種檔案: 選擇多種檔案類型,中間用|隔開 相同的(圖片檔)用;隔開 這樣就可以選擇.txt或者.jpg.png.bmp類型的檔案 ```csharp= ofd.Filter= "Text Files (*.txt)|*.txt|Image Files (*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp|All Files (*.*)|*.*"; ``` ## Multiselect 多檔案選擇 Multiselect為是否允許選擇多個檔案,預設 false ```csharp= ofd.Multiselect = false; // 是否允許選擇多個檔案 ``` ### 應用: 使用foreach開啟每一個檔案 ```csharp= foreach (string filePath in openFileDialog.FileNames) { string ext = Path.GetExtension(filePath).ToLower(); if (ext == ".txt" || ext == ".csv") { .... } else if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".gif") { .... } } ``` ## 判斷是否開啟成功 ```csharp= if (ofd.ShowDialog() == DialogResult.OK) // 顯示對話框並判斷是否點擊 "確定" { MessageBox.Show("開啟成功"); } else { MessageBox.Show("開啟失敗"); } ``` ## FileName 檔案路徑 FileName為使用者選擇的檔案名稱(含路徑) ```csharp= string filePath = ofd.FileName; // 獲取選擇的檔案路徑 MessageBox.Show($"你選擇的檔案是: {filePath}"); ``` ## 檔案類型判斷 ```csharp= string filePath = ofd.FileName; string extension = Path.GetExtension(filePath).ToLower(); if (extension == ".txt") { MessageBox.Show("檔案為txt檔"); } ``` ## FileNames 儲存多個檔案 當 Multiselect = true 時,儲存所有選擇的檔案 ```csharp= OpenFileDialog ofd = new OpenFileDialog Multiselect = true // 開啟多選功能 if (ofd.ShowDialog() == DialogResult.OK) { // 使用 FileNames 獲取所有選擇的檔案 string[] selectedFiles = ofd.FileNames; Foreach (string file in selectedFiles) { MessageBox.Show($"選擇的檔案: {file}"); } } ``` ## InitialDirectory 初始資料夾位置 設定對話框開啟時的初始資料夾 可能一開始點開的是下載,但可以改成在"C:\\Users\\Public" ```csharp= InitialDirectory = @"C:\Users\Public" // 設定初始資料夾 ``` ## CheckFileExists 確保檔案存在 確保檔案存在,預設 true 如果檔案不存在,則會顯示錯誤訊息並阻止用戶繼續操作。 ## 物件使用 openFileDialog也有物件類型,只需將他點出即可 就相差不用宣告,直接用命名的名子來進行 openFileDialog1 = new OpenFileDialog{}來設定基本的 ```csharp= private void BtnSelectImage_Click(object sender, EventArgs e) { openFileDialog1 = new OpenFileDialog { Filter = "圖片檔案 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp", Title = "選擇圖片" }; if (openFileDialog1.ShowDialog() == DialogResult.OK) { MessageBox.Show("上傳成功"); pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); } } ``` ## 儲存圖片 ```csharp= pictureBox1.Image.Save(saveFileDialog1.FileName); ``` # OpenFileDialog and SaveFileDialog差異 功能 | SaveFileDialog (儲存檔案) | OpenFileDialog (打開檔案) -------------------|-------------------------------|--------------------------------- 用途 | 讓使用者選擇儲存路徑並輸入檔名 | 讓使用者選擇要開啟的檔案 允許的操作 | 儲存新檔案(可能覆蓋舊檔) | 開啟現有檔案 按鈕文字 | 預設為「儲存」 | 預設為「開啟」 是否允許選擇已存在的檔案 | 預設允許選擇不存在的檔案 | 只能選擇已存在的檔案 是否有覆蓋提示 | 有(如果檔案已存在,會跳出警告)| 無 功能 | SaveFileDialog | OpenFileDialog -------------------|-----------------------------------|--------------------------------- 用途 | 讓使用者選擇存檔位置 | 讓使用者選擇要開啟的檔案 允許選擇不存在的檔案 | ✅ 是 | ❌ 否 是否有覆蓋警告 | ✅ 預設開啟 | ❌ 無 適用場景 | 存檔案(如 .txt、.json、.csv) | 開啟檔案(如 .jpg、.pdf) 👉 總結 * 儲存檔案時 用 SaveFileDialog,確保使用者輸入檔名並選擇路徑。 * 開啟檔案時 用 OpenFileDialog,確保選擇的是已存在的檔案。 ## save預設存檔類型 ```csharp= saveFileDialog.DefaultExt = "jpg"; // 預設存檔類型 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up