對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2014/10/9 9:26:33
如何:寫入文字檔 (C# 程式設計手冊)
Visual Studio 2013
在下列這些範例中,會示範幾個將文字寫入檔案的方法。 前兩個範例會在 System.IO.File 類別上使用靜態方法,將完整字串陣列或完整字串寫入文字檔。 範例 3 中會示範如何在需要分別處理每一行時,先將文字加入至檔案,然後再寫入檔案。 範例 1 到 3 會覆寫檔案中的全部現有內容。 範例 4 中會示範如何將文字附加至現有的檔案。
範例
C#
這些範例全都會將字串常值寫入至檔案,不過您可能比較想要使用 Format 方法,該方法提供許多控制項讓您撰寫不同類型的值,在欄位中靠右或靠左對齊、使用或不使用邊框間距等等。
編譯程式碼
將程式碼複製至主控台應用程式。
以您電腦上的實際資料夾名稱來取代 "c:\testdir",或以那個名稱來建立資料夾。
穩固程式設計
以下條件可能會造成例外狀況:
該檔案存在而且是唯讀的。
路徑名稱可能太長。
磁碟可能已滿。
請參閱
概念
C# 程式設計手冊
其他資源
檔案系統和登錄 (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# 程式設計手冊)