DateTime.ToFileTime 方法
定义
public:
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 );
}
else
{
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 =
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);
}
}
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, _
fileCreationFileTime)
Else
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.
ToFileTime方法使用 Kind 属性来确定当前 DateTime 对象是否为本地时间、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) 方法 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]