Nối dữ liệu vào tập tin bằng phương thức AppendAllText trong CSharp?(How to append text file with AppendAllText method in CSharp?)


Bài viết này tôi sẽ hướng dẫn cách chèn thêm văn bản(vị trí cuối) vào một tập tin văn bản đã tồn tại bằng phương thức AppendAllText() trong ngôn ngữ lập trình C#.





File.AppendAllText(String, String)

Là một phương thức thuộc lớp File được sử dụng để chèn các chuỗi hay các đoạn văn bảng vào một tập được chỉ định.


Cú pháp - Syntax

public static void AppendAllText(
    string path,
    string contents
)


Ví dụ - Example

Đầu tiên bạn cần chuẩn bị sẵn một tập tin có sẵn nội dụng để thực hành.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AppendFile
{
    class Program
    {
        static void Main(string[] args)
        {

            string path = @"D:\VNLIVES.NET_DEMO.txt";

            // This text is always added, making the file longer over time
            // if it is not deleted.
            string appendText = "This is extra text." + Environment.NewLine;
            File.AppendAllText(path, appendText);

            Console.WriteLine("Append is succed.");
            Console.ReadLine();


        }
    }
}


Kết quả - Result












No comments:

Post a Comment