DateTime.ToFileTime DateTime.ToFileTime DateTime.ToFileTime DateTime.ToFileTime Method


将当前 DateTime 对象的值转换为 Windows 文件时间。Converts the value of the current DateTime object to a Windows file time.

 long ToFileTime();
public long ToFileTime ();
member this.ToFileTime : unit -> int64
Public Function ToFileTime () As Long


表示为 Windows 文件时间的当前 DateTime 对象的值。The value of the current DateTime object expressed as a Windows file time.


所生成的文件时间将表示公元 1601 年 1 月 1 日午夜 12:00 之前的日期和时间。The resulting file time would represent a date and time before 12:00 midnight January 1, 1601 C.E. UTC。UTC.


下面的示例演示 ToFileTime 方法。The following example demonstrates the ToFileTime method.

int main()
   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 );
      __int64 fileCreationFileTime = fileCreationDateTime.ToFileTime();
      System::Console::WriteLine( "{0} in file time is {1}.", fileCreationDateTime, fileCreationFileTime );
      System::Console::WriteLine( "{0} is an invalid file", filePath );

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);
Public Shared Sub Main()

   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


Windows 文件时间是一个64位的值,它表示自公元1900年1月 1601 1 日午夜12:00 之后经过的100纳秒间隔数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. 公元协调世界时(UTC)。(C.E.) Coordinated Universal Time (UTC). 当应用程序创建、访问或写入到文件时,Windows 将使用文件时间来记录。Windows uses a file time to record when an application creates, accesses, or writes to a file.

@No__t-0 方法使用 @no__t 属性来确定当前 @no__t 2 对象是否为本地时间、UTC 时间或被视为本地时间的未指定时间类型。The ToFileTime method uses the Kind property to determine whether the current DateTime object is a local time, a UTC time, or an unspecified kind of time which is treated as a local time.


通常情况下,FromFileTime(Int64) 方法还原 ToFileTime() 方法保存的 @no__t 1 值。Ordinarily, the FromFileTime(Int64) method restores a DateTime value that was saved by the ToFileTime() method. 但是,在下列情况下,这两个值可能不同:However, the two values may differ under the following conditions: -如果 DateTime 值的序列化和反序列化出现在不同的时区中。- If the serialization and deserialization of the DateTime value occur in different time zones. 例如,如果 DateTime 值,时间为 12:30 P.M.。For example, if a DateTime value with a time of 12:30 P.M. 在美国会序列化东部时区,然后在美国太平洋时区,12:30 P.M. 的原始值in the U.S. Eastern Time zone is serialized, and then deserialized in the U.S. Pacific Time zone, the original value of 12:30 P.M. 调整为凌晨9:30。is adjusted to 9:30 A.M. 来反映两个时区之间的差异。to reflect the difference between the two time zones.

-如果序列化的 @no__t 值为0,则表示本地时区中的无效时间。- If the DateTime value that is serialized represents an invalid time in the local time zone. 在这种情况下,ToFileTime() 方法会调整还原的 @no__t 1 值,使其表示本地时区中的有效时间。In this case, the ToFileTime() method adjusts the restored DateTime value so that it represents a valid time in the local time zone.

例如,从标准时间转换为夏时制发生在美国太平洋时区于2010年3月14日上午2:00 点,时间前进一小时,上午3:00For example, the transition from standard time to daylight saving time occurs in the U.S. Pacific Time zone on March 14, 2010, at 2:00 A.M., when the time advances by one hour, to 3:00 A.M. 此小时间隔为无效时间,即此时区中不存在的时间间隔。This hour interval is an invalid time, that is, a time interval that does not exist in this time zone. 下面的示例显示,当处于此范围内的时间由 ToFileTime() 方法转换为长整数值,然后通过 FromFileTime(Int64) 方法还原时,原始值将调整为有效时间。The following example shows that when a time that falls within this range is converted to a long integer value by the ToFileTime() method and is then restored by the FromFileTime(Int64) method, the original value is adjusted to become a valid time. 如示例所示,您可以通过将特定日期和时间值传递到 IsInvalidTime(DateTime) 方法来确定是否可以修改该日期和时间值。You can determine whether a particular date and time value may be subject to modification by passing it to the IsInvalidTime(DateTime) method, as the example illustrates.

[!code-csharpSystem.DateTime.FromFileTime#1] [!code-vbSystem.DateTime.FromFileTime#1][!code-csharpSystem.DateTime.FromFileTime#1] [!code-vbSystem.DateTime.FromFileTime#1]