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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # C# File csv、txt、圖片檔都差不多 將進行檔案一些讀取、寫入等等 結合開啟檔案更好用:[C#Form OpenFileDialog](/QCvjfRroTuK1lpDAzJTM-A) >ChatGPT提供 [TOC] ## using 需要寫道這個,正常來說會自動寫到 ```csharp= using System.IO; ``` ## 創建新文件 (Create) 會創建一個新的資料,如果資料就會覆蓋它 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; // 創建一個文件,如果文件已經存在則會覆蓋 using (FileStream fs = File.Create(filePath)) { // 在這裡你可以進行額外的文件處理 Console.WriteLine("File created successfully."); } ``` ```csharp= string filePath = "test.txt"; // 檔案名稱 File.WriteAllText(filePath, "這是測試內容"); // 建立並寫入內容 Console.WriteLine("TXT 檔案已建立"); ``` ## 修改檔案名(Move) ```csharp= string oldFileName = "test.txt"; //原檔案名稱 string newFileName = "new_test.txt"; //新檔案名稱 File.Move(oldFileName, newFileName); Console.WriteLine("檔案已重新命名"); ``` ## 寫入文件 (Write) 將文字寫入文字,可以覆蓋所有或附加上文字 ### 1. 使用 File.WriteAllText 寫入文本 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; string content = "這是新寫入的內容。"; // 覆蓋文件內容 File.WriteAllText(filePath, content); ``` ### 2. 使用 File.AppendAllText 附加內容 將文字追加在文檔最後,並不改變其遠本的文字 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; string content = "這是追加的內容。"; // 在文件末尾附加文本 File.AppendAllText(filePath, content); ``` ## 讀取文件 (Read) 可以讀取資料裡的文字 讀取檔案的內容,可以顯示於textBox、pictureBox、RichtextBox等等 textBox可以調整是否拖拉,這樣文字多的時候也可以看 RichtextBox可以調整顯示方式(全填滿等等) ### 1. 使用 File.ReadAllText 讀取整個文件內容 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; // 讀取文件的所有內容 string content = File.ReadAllText(filePath); Console.WriteLine(content); ``` ### 2. 使用 File.ReadAllLines 讀取每行內容 將文件的每一行讀取為一個字符串數組。 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; // 讀取文件的所有行 string[] lines = File.ReadAllLines(filePath); foreach (string line in lines) { Console.WriteLine(line); } ``` ### 其他 | 用途 | 方法 | 適用檔案類型 | |--------------|-------------------------------------|--------------------| | 讀取檔案內容 | File.ReadAllText() | .txt, .csv, .json | | 逐行讀取 | File.ReadAllLines() / StreamReader | .txt, .csv | | 寫入 (覆蓋) | File.WriteAllText() | .txt, .csv, .json | | 寫入 (追加) | File.AppendAllText() / StreamWriter(true) | .txt, .csv | | 讀取 JSON | JsonSerializer.Deserialize<> | .json | | 讀取 Word | Microsoft.Office.Interop.Word | .docx | | 讀取 Excel | ClosedXML | .xlsx | | 開啟應用程式 | Process.Start() | .exe, .txt, .pdf, .jpg | * xlsx也可以在csv檔的後面加上 ```csharp= ``` ## 檢查文件是否存在 (Exists) ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; // 檢查文件是否存在 if (File.Exists(filePath)) { Console.WriteLine("文件存在"); } else { Console.WriteLine("文件不存在"); } ``` ## 刪除文件 (Delete) 只能刪除檔案,不能刪除檔案內的某一行內容 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; // 刪除文件 if (File.Exists(filePath)) { File.Delete(filePath); Console.WriteLine("文件已刪除"); } else { Console.WriteLine("文件不存在,無法刪除"); } ``` ## 複製文件 (Copy) 文件從一個位置複製到另一個位置。 ```csharp= using System.IO; string sourcePath = @"C:\path\to\your\file.txt"; string destPath = @"C:\path\to\destination\file.txt"; // 複製文件 File.Copy(sourcePath, destPath, overwrite: true); ``` ## 移動文件 (Move) 將文件從一個位置移動到另一個位置。 ```csharp= using System.IO; string sourcePath = @"C:\path\to\your\file.txt"; string destPath = @"C:\path\to\destination\file.txt"; // 移動文件 File.Move(sourcePath, destPath); ``` ## 獲取文件信息 (FileInfo) FileInfo 類提供了更多文件層級的操作,例如獲取文件大小、創建日期等。 ```csharp= using System.IO; string filePath = @"C:\path\to\your\file.txt"; FileInfo fileInfo = new FileInfo(filePath); // 獲取文件信息 Console.WriteLine($"文件大小: {fileInfo.Length} bytes"); Console.WriteLine($"創建日期: {fileInfo.CreationTime}"); ``` ## pictureBox 開啟照片 ```csharp= string filePath = ofd.FileName; pictureBox1.Image = Image.FromFile(filePath); ``` ## pictureBox 覆蓋原先檔案 我個人覺得第一種方法很好用 | 方法 | 適用情境 | 優點 | |--------------------|------------------------------|----------------| | File.WriteAllBytes() | 直接以位元組覆蓋圖片 | 簡單直接 | | File.Copy() | 新舊圖片都是檔案 | 適合純粹取代 | | Bitmap.Save() | 需要修改圖片內容 | 適合編輯後存回 | ```csharp= File.WriteAllBytes(新檔位置, File.ReadAllBytes(舊檔位置)); File.Copy(新檔位置, 舊檔位置, true); //true 代表允許覆蓋 ``` ## 其他屬性 FileInfo fileInfo = new FileInfo(filePath); 創建 FileInfo 物件,取得檔案的詳細資訊。 * Name:檔案名稱(不包含路徑)。 * FullName:檔案的完整路徑。 * Length:檔案大小(單位:位元組)。 * CreationTime:檔案的建立時間。 * LastWriteTime:檔案的最後修改時間。 * LastAccessTime:檔案的最後存取時間。 ## 修改顏色字體(跳出) * 字體 ![image1](https://hackmd.io/_uploads/rJ4CupM21e.png=50%) ```csharp= using(FontDialog fd = new FontDialog()) { if(fd.ShowDialog() == DialogResult.OK) { richTextBox1.Font = fd.Font; } } ``` * 顏色 ![image](https://hackmd.io/_uploads/B1NCuaG31x.png=50%) ```csharp= using(ColorDialog cd = new ColorDialog()) { if(cd.ShowDialog() == DialogResult.OK) { richTextBox1.ForeColor = cd.Color; } } ```

    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