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


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