當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# DateTime.ToFileTime()用法及代碼示例


此方法用於將當前DateTime對象的值轉換為Windows文件時間。

用法: public long ToFileTime ();

返回值:此方法返回表示為Windows文件時間的當前DateTime對象的值。


異常:如果結果文件時間表示的是UTC時間1601年1月1日午夜12:00之前的日期和時間,則此方法將提供ArgumentOutOfRangeException。

以下示例程序旨在說明DateTime.ToFileTime()方法的使用:

示例1:

// C# program to demonstrate the 
// DateTime.ToFileTime() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // creating object of DateTime 
            DateTime date = new DateTime(2011, 1, 
                                    1, 4, 0, 15); 
  
            // converting current DateTime  
            // object to a Windows file time. 
            // using ToFileTime() method; 
            long value = date.ToFileTime(); 
  
            // Display the TimeSpan 
            Console.WriteLine("Windows file time "+ 
                                  "is {0}", value); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Windows file time is 129383280150000000

示例2:對於ArgumentOutOfRangeException

// C# program to demonstrate the 
// DateTime.ToFileTime() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // creating object of DateTime 
            DateTime date = new DateTime(1600, 1, 
                                    1, 4, 0, 15); 
  
            // converting current DateTime 
            // object to a Windows file time. 
            // using ToFileTime() method; 
            long value = date.ToFileTime(); 
  
            // Display the TimeSpan 
            Console.WriteLine("Windows file time"+ 
                                " is {0}", value); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Exception Thrown: System.ArgumentOutOfRangeException

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTime.ToFileTime() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。