File.AppendAllLines(String, IEnumerable<String>) 是一個內置的 File 類方法,用於將指定的行附加到文件然後關閉文件。
用法:
public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<String> contents);
參數:此函數接受兩個參數,如下所示:
- 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.
異常:
- ArgumentException:路徑是一個零長度的字符串,隻包含空格,或者包含一個由 GetInvalidPathChars() 方法定義的無效字符。
- ArgumentNullException:路徑或內容為空。
- DirectoryNotFoundException:路徑無效,即目錄不存在或位於未映射的驅動器上。
- FileNotFoundException:找不到路徑指定的文件。
- IOException:打開文件時發生I /O錯誤。
- PathTooLongException:路徑超過 system-defined 最大長度。
- NotSupportedException:路徑格式無效。
- SecurityException:調用者沒有寫入文件的權限。
- UnauthorizedAccessException:路徑指定隻讀文件。 OR 當前平台不支持此操作。或者路徑是一個目錄。
下麵是說明 File.AppendAllLines() 方法的程序。
程序1:使用了兩個文件,一個是file.txt,另一個是gfg.txt,在運行程序之前,其內容如下所示。
// C# program to illustrate the usage
// of File.AppendAllLines() method
// Using System, System.IO, and
// System.Linq namespaces
using System;
using System.IO;
using System.Linq;
// 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);
// 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, and
// System.Linq namespaces
using System;
using System.IO;
using System.Linq;
// 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);
// 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# 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()用法及代碼示例
- C# Object.GetTypeCode()用法及代碼示例
- C# Array.BinarySearch(Array, Int32, Int32, Object)用法及代碼示例
- C# Stack.ToString()用法及代碼示例
- C# Graphics.Clear()用法及代碼示例
- C# Type.GetConstructors()用法及代碼示例
- C# List.FindIndex()用法及代碼示例
- C# Double.Equals()用法及代碼示例
- C# Decimal.GetTypeCode用法及代碼示例
- C# Decimal.GetHashCode用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 File.AppendAllLines(String, IEnumerable<String>) Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。