Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Chương trình C# ghi mảng vào file

File.WriteAllLines(path, string[]) ghi từng phần tử mảng thành một dòng trong file (tự động thêm newline).

Ví dụ

using System.IO;
using System;

public class Program
{
    public static void Main()
    {
        string[] stringArray = { "one", "two", "three" };
        File.WriteAllLines("new.txt", stringArray);

        using (StreamReader sr = new StreamReader("new.txt"))
        {
            string res = sr.ReadToEnd();
            Console.WriteLine(res);
        }
    }
}

Kết quả

one
two
three