DateTime.FromFileTime(Int64) DateTime.FromFileTime(Int64) DateTime.FromFileTime(Int64) DateTime.FromFileTime(Int64) Method

定义

将指定的 Windows 文件时间转换为等效的本地时间。Converts the specified Windows file time to an equivalent local time.

public:
 static DateTime FromFileTime(long fileTime);
public static DateTime FromFileTime (long fileTime);
static member FromFileTime : int64 -> DateTime
Public Shared Function FromFileTime (fileTime As Long) As DateTime

参数

fileTime
Int64 Int64 Int64 Int64

以计时周期表示的 Windows 文件时间。A Windows file time expressed in ticks.

返回

一个表示本地时间的对象,等效于由 fileTime 参数表示的日期和时间。An object that represents the local time equivalent of the date and time represented by the fileTime parameter.

异常

fileTime 小于零或表示大于 MaxValue 的时间。fileTime is less than 0 or represents a time greater than MaxValue.

示例

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

System::TimeSpan FileAge( long fileCreationTime )
{
   System::DateTime now = System::DateTime::Now;
   try
   {
      System::DateTime fCreationTime =
         System::DateTime::FromFileTime( fileCreationTime );
      System::TimeSpan fileAge = now.Subtract( fCreationTime );
      return fileAge;
   }
   catch ( ArgumentOutOfRangeException^ ) 
   {
      // fileCreationTime is not valid, so re-throw the exception.
      throw;
   }
}
public System.TimeSpan FileAge(long fileCreationTime) {

    System.DateTime now = System.DateTime.Now;
    try {
        System.DateTime fCreationTime = 
            System.DateTime.FromFileTime(fileCreationTime);
        System.TimeSpan fileAge = now.Subtract(fCreationTime);
        return fileAge;				
    } 
    catch (ArgumentOutOfRangeException) {
        // fileCreationTime is not valid, so re-throw the exception.
        throw;
    }
}
Public Function FileAge(ByVal fileCreationTime As Long) As System.TimeSpan
   Dim now As System.DateTime
   now = System.DateTime.Now

   Try
      Dim fCreationTime As System.DateTime
      Dim fAge As System.TimeSpan
      fCreationTime = System.DateTime.FromFileTime(fileCreationTime)
      fAge = now.Subtract(fCreationTime)
      Return fAge
   Catch exp As ArgumentOutOfRangeException
      ' fileCreationTime is not valid, so re-throw the exception.
      Throw
   End Try
End Function

注解

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 参数指定以100毫微秒计时周期表示的文件时间。The fileTime parameter specifies a file time expressed in 100-nanosecond ticks.

从 .NET Framework 版本2.0 开始,返回值为 DateTime,其 @no__t 属性是 DateTimeKind.LocalStarting with the .NET Framework version 2.0, the return value is a DateTime whose Kind property is DateTimeKind.Local.

调用方说明

通常情况下,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]

适用于

另请参阅