File.AppendAllLines(String, IEnumerable<String>, Encoding) 是一個內置的 File 類方法,用於使用指定的編碼將指定的行附加到文件中,然後關閉文件。如果指定的文件不存在,此方法將創建一個新文件,將指定的行寫入該文件,然後關閉該文件。
用法:
public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<String> contents, System.Text.Encoding encoding);
參數:此函數接受兩個參數,如下所示:
- path: This is the file where lines are going to be appended. The file is created if it doesn’t already exist.
- contents: This is the specified contents which is to be appended to the file.
- encoding: This is the specified character encoding.
異常:
- ArgumentException:路徑是一個長度為零的字符串,隻包含空格,或一個由 GetInvalidPathChars() 方法定義的無效字符。
- ArgumentNullException:路徑、內容或編碼為空。
- DirectoryNotFoundException:路徑無效,即目錄不存在或位於未映射的驅動器上。
- FileNotFoundException:找不到路徑給出的文件。
- IOException:打開文件時發生I /O錯誤。
- PathTooLongException:路徑超過 system-defined 最大長度。
- NotSupportedException:路徑格式無效。
- SecurityException:調用者沒有所需的權限。
- UnauthorizedAccessException:路徑指定隻讀文件。或 當前平台不支持此操作。或者路徑是一個目錄。或者調用者沒有所需的權限。
下麵是說明 File.AppendAllLines(String, IEnumerable, Encoding) 方法的程序。
程序1:使用了兩個文件,一個是file.txt,另一個是gfg.txt,在運行程序之前,其內容如下所示。
// C# program to illustrate the usage
// of File.AppendAllLines() method
// Using System, System.IO,
// System.Linq and System.Text namespaces
using System;
using System.IO;
using System.Linq;
using System.Text;
// Creating class
class GfG {
// Creating a file
static string myfile = @"file.txt";
// Main method
static void Main(string[] args)
{
// Reading lines of the file created above
var appendTofile = from line in File.ReadLines(myfile)
// Using select statement
select line;
// Calling AppendAllLines() method with its
// parameters
File.AppendAllLines(@"gfg.txt", appendTofile, Encoding.UTF8);
// Printed when the stated file is appended
Console.WriteLine("All lines are appended");
}
}
執行:
mcs -out:main.exe main.cs mono main.exe All lines are appended
運行上述代碼後,將顯示如上輸出,文件 gfg.txt 的內容如下所示,這意味著 file.txt 的內容已附加到文件 gfg.txt
程序2:僅創建了一個文件 file.txt,其內容如下所示:
// C# program to illustrate the usage
// of File.AppendAllLines() method
// Using System, System.IO,
// System.Linq and System.Text namespaces
using System;
using System.IO;
using System.Linq;
using System.Text;
// Creating class
class GfG {
// Creating a file
static string myfile = @"file.txt";
// Main method
static void Main(string[] args)
{
// Reading lines of the file created above
var appendTofile = from line in File.ReadLines(myfile)
// It only appends the line that starts with g
where(line.StartsWith("g"))
// Using select statement
select line;
// Calling AppendAllLines() method with its
// parameters
File.AppendAllLines(@"gfg.txt", appendTofile, Encoding.UTF8);
// Printed when the stated file is appended
Console.WriteLine("All lines are appended");
}
}
執行:
mcs -out:main.exe main.cs mono main.exe All lines are appended
運行上麵的代碼後,將顯示上麵的輸出,它會創建一個名為 gfg.txt 的新文件,其內容與文件 file.txt 相同:
相關用法
- C# File.AppendAllText(String, String, Encoding)用法及代碼示例
- C# File.ReadAllLines(String, Encoding)用法及代碼示例
- C# File.ReadLines(String, Encoding)用法及代碼示例
- C# File.ReadAllText(String, Encoding)用法及代碼示例
- C# File.WriteAllText(String, String, Encoding)用法及代碼示例
- C# File.WriteAllLines(String, String[], Encoding)用法及代碼示例
- C# File.WriteAllLines(String, IEnumerable<String>, Encoding)用法及代碼示例
- C# Array.GetValue()方法用法及代碼示例
- C# File.GetLastWriteTimeUtc()用法及代碼示例
- C# MathF.Sin()用法及代碼示例
- C# Double.CompareTo用法及代碼示例
- C# UInt16.GetHashCode用法及代碼示例
- C# Int64.CompareTo用法及代碼示例
- C# MathF.Truncate()用法及代碼示例
- C# MathF.Exp()用法及代碼示例
- C# Array.BinarySearch(Array, Object)用法及代碼示例
- C# Char.GetHashCode()用法及代碼示例
- C# Char.GetTypeCode()用法及代碼示例
- C# Object.GetHashCode()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 File.AppendAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。