DateTime.ToFileTime 方法

定義

將目前 DateTime 物件的值轉換成 Windows 檔案時間。Converts the value of the current DateTime object to a Windows file time.

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.

範例

下列範例示範 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位的值,代表自西元1601年1月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) 方法會還原 ToFileTime() 方法所儲存的 DateTime 值。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:30is 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-csharpsystem.web. FromFileTime # 1][! code-vbSystem.web FromFileTime # 1][!code-csharpSystem.DateTime.FromFileTime#1] [!code-vbSystem.DateTime.FromFileTime#1]

適用於

另請參閱