當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# File.AppendText()用法及代碼示例

File.AppendText()是內置的File類方法,該方法用於創建StreamWriter,該StreamWriter將UTF-8編碼的文本附加到現有文件中,否則,如果指定的文件不存在,它將創建一個新文件。

用法:

public static System.IO.StreamWriter AppendText (string path);

參數:該函數接受如下所示的參數:



  • path: This is the file where UTF-8 encoded texts are going to be appended. The file is created if it doesn’t already exist.

異常

  • UnauthorizedAccessException:調用者沒有所需的權限。
  • ArgumentException:路徑是長度為零的字符串,僅包含空格,或者包含一個或多個InvalidPathChars定義的無效字符。
  • ArgumentNullException:路徑為空。
  • PathTooLongException:給定的路徑,文件名或兩者都超過了system-defined的最大長度。
  • DirectoryNotFoundException:指定的路徑無效,即目錄不存在或位於未映射的驅動器上。
  • NotSupportedException:路徑格式無效。

返回值:返回將指定的UTF-8編碼文本附加到指定文件或新文件的流編寫器。

下麵是說明File.AppendText()方法的程序。

程序1:在運行以下代碼之前,將創建一個文件file.txt,其中包含一些內容,如下所示:

file.txt

// C# program to illustrate the usage 
// of File.AppendText() method 
  
// Using System, System.IO namespaces 
using System; 
using System.IO; 
  
class GFG { 
    // Main method 
    public static void Main() 
    { 
        // Creating a file 
        string myfile = @"file.txt"; 
  
        // Appending the given texts 
        using(StreamWriter sw = File.AppendText(myfile)) 
        { 
            sw.WriteLine("Gfg"); 
            sw.WriteLine("GFG"); 
            sw.WriteLine("GeeksforGeeks"); 
        } 
  
        // Opening the file for reading 
        using(StreamReader sr = File.OpenText(myfile)) 
        { 
            string s = ""; 
            while ((s = sr.ReadLine()) != null) { 
                Console.WriteLine(s); 
            } 
        } 
    } 
}

執行中:

mcs -out:main.exe main.cs
mono main.exe
Geeks
Gfg
GFG
GeeksforGeeks

運行以上代碼後,將顯示以上輸出,並且現有文件file.txt如下所示:

程序2:最初,沒有創建文件,因此下麵的代碼本身創建了一個名為file.txt的文件

// C# program to illustrate the usage 
// of File.AppendText() method 
  
// Using System, System.IO namespaces 
using System; 
using System.IO; 
  
class GFG { 
    // Main method 
    public static void Main() 
    { 
        // Creating a file 
        string myfile = @"file.txt"; 
  
        // Checking the above file 
        if (!File.Exists(myfile)) { 
            // Creating the same file if it doesn't exist 
            using(StreamWriter sw = File.CreateText(myfile)) 
            { 
                sw.WriteLine("GeeksforGeeks"); 
                sw.WriteLine("is"); 
                sw.WriteLine("a"); 
            } 
        } 
  
        // Appending the given texts 
        using(StreamWriter sw = File.AppendText(myfile)) 
        { 
            sw.WriteLine("computer"); 
            sw.WriteLine("science"); 
            sw.WriteLine("portal."); 
        } 
  
        // Opening the file for reading 
        using(StreamReader sr = File.OpenText(myfile)) 
        { 
            string s = ""; 
            while ((s = sr.ReadLine()) != null) { 
                Console.WriteLine(s); 
            } 
        } 
    } 
}

執行中:

mcs -out:main.exe main.cs
mono main.exe
GeeksforGeeks
is
a
computer
science
portal.

運行上述代碼後,將創建一個新文件file.txt,如下所示:

file.txt




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 File.AppendText() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。