Convert the value of the current DateTime object to a Windows file time in C#



To convert the value of the current DateTime object to a Windows file time, the code is as follows −

Example

 Live Demo

using System;
public class Demo {
   public static void Main() {
      DateTime d = DateTime.Now;
      Console.WriteLine("Date = {0}", d);
      long res = d.ToFileTime();
      Console.WriteLine("Windows file time = {0}", res);
   }
}

Output

This will produce the following output −

Date = 10/16/2019 8:17:26 AM
Windows file time = 132156874462559390

Example

Let us see another example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 05, 10, 6, 10, 25);
      Console.WriteLine("Date = {0}", d);
      long res = d.ToFileTime();
      Console.WriteLine("Windows file time = {0}", res);
   }
}

Output

This will produce the following output −

Date = 5/10/2019 6:10:25 AM
Windows file time = 132019422250000000

Advertisements