DateTime.FromFileTime(Int64) 方法
定义
将指定的 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
以计时周期表示的 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.
例外
示例
下面的示例演示 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.
fileTime参数指定以100毫微秒计时周期表示的文件时间。The fileTime parameter specifies a file time expressed in 100-nanosecond ticks.
从 .NET Framework 版本2.0 开始,返回值为, DateTime 其 Kind 属性为 DateTimeKind.Local 。Starting with the .NET Framework version 2.0, the return value is a DateTime whose Kind property is DateTimeKind.Local.
调用方说明
通常, FromFileTime(Int64) 方法 DateTime 会还原由方法保存的值 ToFileTime() 。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 时区中反序列化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.
-如果 DateTime 序列化的值表示本地时区中的无效时间,则为。- If the DateTime value that is serialized represents an invalid time in the local time zone. 在这种情况下, ToFileTime() 方法会调整还原的 DateTime 值,使其表示本地时区中的有效时间。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-csharpFromFileTime # 1][! code-vbFromFileTime # 1][!code-csharpSystem.DateTime.FromFileTime#1] [!code-vbSystem.DateTime.FromFileTime#1]