Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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
[C#]
public long ToFileTime();
[C++]
public: __int64 ToFileTime();
[JScript]
public function ToFileTime() : long;

Return Value

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

Exceptions

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

Remarks

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).

Example

[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, _
                               fileCreationFileTime)
   Else
      System.Console.WriteLine("{0} is an invalid file", filePath)
   End If
End Sub

[C#] 
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 = 
            System.IO.File.GetCreationTime(filePath);

        long fileCreationFileTime = fileCreationDateTime.ToFileTime();

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

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

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

      __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.

Requirements

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft