在 C# 中获取文件的创建时间

csharpprogrammingserver side programming更新于 2025/5/23 3:07:17

要在 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() {
     使用 (StreamWriter sw = new StreamWriter("qa.txt")) {
         sw.WriteLine("Questions and Answers!");
      }
      FileInfo file = new FileInfo("qa.txt");
      // 文件创建时间
      DateTime dt = file.CreationTime;
      Console.WriteLine(dt);
   }
}

输出

9/5/2018 5:20:03 AM

相关文章