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


C# DateTimeOffset.FromFileTime()用法及代碼示例


DateTimeOffset.FromFileTime(Int64)方法用於將指定的Windows文件時間轉換為等效的本地時間。

用法: public static DateTimeOffset FromFileTime (long fileTime);
Here, it takes a Windows file time, expressed in ticks.

返回值:此方法返回一個表示fileTime的日期和時間的對象,其偏移量設置為本地時間偏移量。


異常:如果文件時間小於零或文件時間大於DateTimeOffset.MaxValue.Ticks,則此方法將提供ArgumentOutOfRangeException。

以下示例程序旨在說明DateTimeOffset.FromFileTime(Int64)方法的用法:

示例1:

// C# program to demonstrate the 
// DateTimeOffset.FromFileTime(Int64) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // converts the specified Windows file time 
            // to an equivalent local time. 
            // instance using FromFileTime() method 
            DateTimeOffset value = DateTimeOffset.FromFileTime(10000); 
  
            // Display the time 
            Console.WriteLine("DateTimeOffset is {0}", value); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
DateTimeOffset is 01/01/1601 00:00:00 +00:00

示例2:對於ArgumentOutOfRangeException

// C# program to demonstrate the 
// DateTimeOffset.FromFileTime(Int64) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // converts the specified Windows file time 
            // to an equivalent local time. 
            // instance using FromFileTime() method 
            DateTimeOffset value = DateTimeOffset.FromFileTime(-1); 
  
            // Display the time 
            Console.WriteLine("DateTimeOffset is {0}", value); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Exception Thrown: System.ArgumentOutOfRangeException

參考:



相關用法


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