Export (0) Print
Expand All

DateTime.ToFileTime Method

Converts the value of this instance to the format of a local operating system file time.

[Visual Basic]
Public Function ToFileTime() As Long
public long ToFileTime();
public: __int64 ToFileTime();
public function ToFileTime() : long;

Return Value

The value of this DateTime in the format of a local operating system file time.


Exception Type Condition
ArgumentOutOfRangeException The value of this instance cannot be converted to a system file time.


A system file time is a 64-bit unsigned value representing the date and time as the number of 100-nanosecond intervals that have elapsed since January 1, 1601 12:00 A.M.

An exception is thrown if you attempt to convert a date earlier than January 1, 1601 12:00:00 A.M. coordinated universal time (UTC).


[Visual Basic, C#, C++] The following sample demonstrates ToFileTime.

[Visual Basic] 
Overloads Shared Sub Main(ByVal args() As String)

   System.Console.WriteLine("Enter the file path:")
   Dim filePath As String
   filePath = System.Console.ReadLine()

   If System.IO.File.Exists(filePath) Then
      Dim fileCreationDateTime As System.DateTime
      fileCreationDateTime = System.IO.File.GetCreationTime(filePath)

      Dim fileCreationFileTime As Long
      fileCreationFileTime = fileCreationDateTime.ToFileTime()

      System.Console.WriteLine("{0} in file time is {1}.", _
                               fileCreationDateTime, _
      System.Console.WriteLine("{0} is an invalid file", filePath)
   End If
End Sub

static void Main(string[] args)
    System.Console.WriteLine("Enter the file path:");
    string filePath = System.Console.ReadLine();

    if (System.IO.File.Exists(filePath)) {
        System.DateTime fileCreationDateTime = 

        long fileCreationFileTime = fileCreationDateTime.ToFileTime();

        System.Console.WriteLine("{0} in file time is {1}.",
    else {
        System.Console.WriteLine("{0} is an invalid file", filePath);

int main() {
   System::Console::WriteLine(S"Enter the file path:");
   String* filePath = System::Console::ReadLine();

   if (System::IO::File::Exists(filePath)) {
      System::DateTime fileCreationDateTime =

      __int64 fileCreationFileTime = fileCreationDateTime.ToFileTime();

      System::Console::WriteLine(S"{0} in file time is {1}.", __box(fileCreationDateTime), __box(fileCreationFileTime));
   } else {
      System::Console::WriteLine(S"{0} is an invalid file", filePath);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DateTime Structure | DateTime Members | System Namespace | ToFileTimeUtc | Int64 | ToUniversalTime | GetUtcOffset

© 2015 Microsoft