Khởi tạo mảng string:
string[] names = new string[] {"Jack", "Tom"};
Duyệt foreach ghi vào file:
using (StreamWriter sw = new StreamWriter("names.txt")) {
foreach (string s in names) sw.WriteLine(s);
}
Ví dụ
using System;
using System.IO;
namespace FileApplication {
class Program {
static void Main(string[] args) {
string[] names = new string[] {"Jack", "Tom"};
using (StreamWriter sw = new StreamWriter("names.txt")) {
foreach (string s in names) sw.WriteLine(s);
}
string line = "";
using (StreamReader sr = new StreamReader("names.txt")) {
while ((line = sr.ReadLine()) != null) Console.WriteLine(line);
}
Console.ReadKey();
}
}
}