DateTimeOffset.FromFileTime Method (Int64)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Converts the specified Windows file time to an equivalent local time.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static DateTimeOffset FromFileTime(
	long fileTime


Type: System.Int64

A Windows file time, expressed in ticks.

Return Value

Type: System.DateTimeOffset

An object that represents the date and time of fileTime with the offset set to the local time offset.

Exception Condition

filetime is less than zero.


filetime is greater than DateTimeOffset.MaxValue.Ticks.

A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC). Windows uses a file time to record when an application creates, accesses, or writes to a file.

A Windows file time is directly accessible through the Windows API by calling the GetFileTime function, which returns a FILETIME structure. The single function parameter is the handle of the file whose file time information is to be retrieved. The file handle is retrieved by calling the CreateFile function. The FILETIME structure's dwHighDateTime member contains the four high-order bytes of the file time, and its dwLowDateTime member contains the four low-order bytes. The example that follows illustrates how to retrieve Windows file time values and convert them to DateTimeOffset values.

Windows file time values can also be created from DateTime values by calling the DateTime.ToFileTime and DateTime.ToFileTimeUtc methods, and from DateTimeOffset values by calling the DateTimeOffset.ToFileTime method.

The following example uses the Windows API to retrieve the Windows file times for the WordPad executable.

using System;
using System.IO;
using System.Runtime.InteropServices;

public struct FileTime
   public uint dwLowDateTime;
   public uint dwHighDateTime;

   public static implicit operator long(FileTime fileTime)
      long returnedLong;
      // Convert 4 high-order bytes to a byte array
      byte[] highBytes = BitConverter.GetBytes(fileTime.dwHighDateTime);
      // Resize the array to 8 bytes (for a Long)
      Array.Resize(ref highBytes, 8); 

      // Assign high-order bytes to first 4 bytes of Long
      returnedLong = BitConverter.ToInt64(highBytes, 0); 
      // Shift high-order bytes into position
      returnedLong = returnedLong << 32;
      // Or with low-order bytes
      returnedLong = returnedLong | fileTime.dwLowDateTime;
      // Return long 
      return returnedLong;

public class FileTimes
   private const int OPEN_EXISTING = 3;
   private const int INVALID_HANDLE_VALUE = -1;

   [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
   private static extern int CreateFile(string lpFileName, 
                                       int dwDesiredAccess, 
                                       int dwShareMode, 
                                       int lpSecurityAttributes, 
                                       int dwCreationDisposition, 
                                       int dwFlagsAndAttributes, 
                                       int hTemplateFile);

   private static extern bool GetFileTime(int hFile, 
                                          out FileTime lpCreationTime, 
                                          out FileTime lpLastAccessTime, 
                                          out FileTime lpLastWriteTime);

   private static extern bool CloseHandle(int hFile); 

   public static void Main()
      // Open file %windir%\write.exe
      string winDir = Environment.SystemDirectory; 
      if (! (winDir.EndsWith(Path.DirectorySeparatorChar.ToString())))
         winDir += Path.DirectorySeparatorChar; 
      winDir += "write.exe";

      // Get file time using Windows API
      // Open file
      int hFile = CreateFile(winDir, 0, 0, 0, OPEN_EXISTING, 0, 0);
      if (hFile == INVALID_HANDLE_VALUE)
         Console.WriteLine("Unable to access {0}.", winDir);
         FileTime creationTime, accessTime, writeTime;
         if (GetFileTime(hFile, out creationTime, out accessTime, out writeTime)) 
            long fileCreationTime = (long) creationTime;
            long fileAccessTime = accessTime;
            long fileWriteTime = (long) writeTime;

            Console.WriteLine("File {0} Retrieved Using the Windows API:", winDir);
            Console.WriteLine("   Created:     {0:d}", DateTimeOffset.FromFileTime(fileCreationTime).ToString());
            Console.WriteLine("   Last Access: {0:d}", DateTimeOffset.FromFileTime(fileAccessTime).ToString());
            Console.WriteLine("   Last Write:  {0:d}", DateTimeOffset.FromFileTime(fileWriteTime).ToString());

      // Get date and time, convert to file time, then convert back
      FileInfo fileInfo = new FileInfo(winDir);
      DateTimeOffset infoCreationTime, infoAccessTime, infoWriteTime;
      long ftCreationTime, ftAccessTime, ftWriteTime;

      // Get dates and times of file creation, last access, and last write
      infoCreationTime = fileInfo.CreationTime;
      infoAccessTime = fileInfo.LastAccessTime;
      infoWriteTime = fileInfo.LastWriteTime;
      // Convert values to file times
      ftCreationTime = infoCreationTime.ToFileTime();
      ftAccessTime = infoAccessTime.ToFileTime();
      ftWriteTime = infoWriteTime.ToFileTime();

      // Convert file times back to DateTimeOffset values
      Console.WriteLine("File {0} Retrieved Using a FileInfo Object:", winDir);
      Console.WriteLine("   Created:     {0:d}", DateTimeOffset.FromFileTime(ftCreationTime).ToString());
      Console.WriteLine("   Last Access: {0:d}", DateTimeOffset.FromFileTime(ftAccessTime).ToString());
      Console.WriteLine("   Last Write:  {0:d}", DateTimeOffset.FromFileTime(ftWriteTime).ToString()); 
// The example produces the following output:
//    File C:\WINDOWS\system32\write.exe Retrieved Using the Windows API:
//       Created:     10/13/2005 5:26:59 PM -07:00
//       Last Access: 3/20/2007 2:07:00 AM -07:00
//       Last Write:  8/4/2004 5:00:00 AM -07:00
//    File C:\WINDOWS\system32\write.exe Retrieved Using a FileInfo Object:
//       Created:     10/13/2005 5:26:59 PM -07:00
//       Last Access: 3/20/2007 2:07:00 AM -07:00
//       Last Write:  8/4/2004 5:00:00 AM -07:00

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top