獲取 C# 中檔案建立的時間


如要在 C# 中獲取檔案建立的時間,請使用 CreationTime() 方法。

為此,使用 FileInfo 和 DateTime 類,建立每個類的物件 −

FileInfo file = new FileInfo("new.txt");
DateTime dt = file.CreationTime;

讓我們看看完整的程式碼 −

示例

 即時演示

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");
      // file creation time
      DateTime dt = file.CreationTime;
      Console.WriteLine(dt);
   }
}

輸出

9/5/2018 5:20:03 AM

更新時間: 22-6 月-2020

2K+ 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.