获取 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.