Sử dụng phương thức WriteAllLines để ghi một mảng vào một tệp.
Đầu tiên, đặt một mảng chuỗi -
string[] stringArray = new string[] {
"one",
"two",
"three"
}; Bây giờ, hãy sử dụng phương thức WriteAllLines để thêm mảng trên vào một tệp -
File.WriteAllLines("new.txt", stringArray); Đây là mã hoàn chỉnh -
Ví dụ
using System.IO;
using System;
public class Program {
public static void Main() {
string[] stringArray = new string[] {
"one",
"two",
"three"
};
File.WriteAllLines("new.txt", stringArray);
using (StreamReader sr = new StreamReader("new.txt")) {
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}
} Đầu ra
one two three