Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# ดูเวลาเขียนไฟล์ล่าสุด

ใช้ FileInfo.LastWriteTime ดึงวันเวลาที่ไฟล์ถูกแก้ไข/เขียนล่าสุด

ตัวอย่างโค้ด (รวม Creation, LastAccess, LastWrite)

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        using (StreamWriter sw = new StreamWriter("amit.txt"))
        {
            sw.WriteLine("Welcome!");
        }
        
        FileInfo file = new FileInfo("amit.txt");
        DateTime dt = file.CreationTime;
        Console.WriteLine("Creation: " + dt);
        
        dt = file.LastAccessTime;
        Console.WriteLine("LastAccess: " + dt);
        
        dt = file.LastWriteTime;
        Console.WriteLine("LastWrite: " + dt);
    }
}

ผลลัพธ์ (ตัวอย่าง)

Creation: 9/5/2018 5:17:27 AM
LastAccess: 9/5/2018 5:17:27 AM
LastWrite: 9/5/2018 5:17:27 AM

คุณสมบัติ Utc: มี CreationTimeUtc, LastAccessTimeUtc, LastWriteTimeUtc สำหรับเวลา UTC