茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729345 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00126.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

Dot Net? : [轉貼]C# 讀寫 txt 檔的兩種方法介紹

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]如何:寫入文字檔 (C# 程式設計手冊)
如何:寫入文字檔 (C# 程式設計手冊)
Visual Studio 2013

在下列這些範例中,會示範幾個將文字寫入檔案的方法。 前兩個範例會在 System.IO.File 類別上使用靜態方法,將完整字串陣列或完整字串寫入文字檔。 範例 3 中會示範如何在需要分別處理每一行時,先將文字加入至檔案,然後再寫入檔案。 範例 1 到 3 會覆寫檔案中的全部現有內容。 範例 4 中會示範如何將文字附加至現有的檔案。
範例
C#
class WriteTextFile
{
    static void Main()
    {

        // These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
        // You can modify the path if necessary.

        // Example #1: Write an array of strings to a file.
        // Create a string array that consists of three lines.
        string[] lines = { "First line", "Second line", "Third line" };
        // WriteAllLines creates a file, writes a collection of strings to the file,
        // and then closes the file.
        System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);


        // Example #2: Write one string to a text file.
        string text = "A class is the most powerful data type in C#. Like a structure, " +
                       "a class defines the data and behavior of the data type. ";
        // WriteAllText creates a file, writes the specified string to the file,
        // and then closes the file.
        System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

        // Example #3: Write only some strings in an array to a file.
        // The using statement automatically closes the stream and calls
        // IDisposable.Dispose on the stream object.
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
        {
            foreach (string line in lines)
            {
                // If the line doesn't contain the word 'Second', write the line to the file.
                if (!line.Contains("Second"))
                {
                    file.WriteLine(line);
                }
            }
        }

        // Example #4: Append new text to an existing file.
        // The using statement automatically closes the stream and calls
        // IDisposable.Dispose on the stream object.
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
        {
            file.WriteLine("Fourth line");
        }
    }
}
 //Output (to WriteLines.txt):
 //   First line
 //   Second line
 //   Third line

 //Output (to WriteText.txt):
 //   A class is the most powerful data type in C#. Like a structure, a class defines the data and behavior of the data type.

 //Output to WriteLines2.txt after Example #3:
 //   First line
 //   Third line

 //Output to WriteLines2.txt after Example #4:
 //   First line
 //   Third line
 //   Fourth line


這些範例全都會將字串常值寫入至檔案,不過您可能比較想要使用 Format 方法,該方法提供許多控制項讓您撰寫不同類型的值,在欄位中靠右或靠左對齊、使用或不使用邊框間距等等。
編譯程式碼

將程式碼複製至主控台應用程式。

以您電腦上的實際資料夾名稱來取代 "c:\testdir",或以那個名稱來建立資料夾。
穩固程式設計

以下條件可能會造成例外狀況:

該檔案存在而且是唯讀的。

路徑名稱可能太長。

磁碟可能已滿。

請參閱
概念
C# 程式設計手冊
其他資源
檔案系統和登錄 (C# 程式設計手冊)
如何將自訂物件集合儲存至本機存放區

原文出處:如何:寫入文字檔 (C# 程式設計手冊)
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|