当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。