anna0212
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 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"; // 預設存檔類型 ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    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

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully