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

Lấy thời gian tạo file trong C#

Sử dụng FileInfo.CreationTime để lấy ngày giờ file được tạo.

Ví dụ

using System.IO;
using System;

public class Program
{
    public static void Main()
    {
        using (StreamWriter sw = new StreamWriter("qa.txt"))
        {
            sw.WriteLine("Questions and Answers!");
        }
        FileInfo file = new FileInfo("qa.txt");
        DateTime dt = file.CreationTime;
        Console.WriteLine(dt);
    }
}

Kết quả

9/5/2018 5:20:03 AM

Lưu ý: Thời gian phụ thuộc vào hệ thống file và múi giờ. Dùng CreationTimeUtc cho UTC.