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


C# File.GetCreationTime()用法及代码示例


File.GetCreationTime(String)是内置的File类方法,该方法用于返回指定文件或目录的创建日期和时间。

用法:

public static DateTime GetCreationTime (string path);

参数:该函数接受如下所示的参数:



  • path: This is the specified file path whose creation date and time is going to be returned.

异常:

  • UnauthorizedAccessException:调用者没有所需的权限。
  • ArgumentException:路径为长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径为空。
  • PathTooLongException:给定的路径,文件名或两者都超过了system-defined的最大长度。
  • NotSupportedException:路径格式无效。

返回值:返回指定文件或目录的创建日期和时间。

下面是说明File.GetCreationTime(String)方法的程序。

程序1:在运行下面的代码之前,将创建一个文件file.txt,其内容如下所示:

file.txt

// C# program to illustrate the usage 
// of File.GetCreationTime(String) method 
  
// Using System and System.IO namespaces 
using System; 
using System.IO; 
  
class GFG { 
    static void Main(string[] args) 
    { 
        // Calling the GetCreationTime() function 
        DateTime fileCreatedDate = File.GetCreationTime(@"file.txt"); 
  
        // Printing the creation date and time of the 
        // specified file 
        Console.WriteLine("File created on:" + fileCreatedDate); 
    } 
}

执行中:

File created on:4/19/2020 4:02:54 AM

程序2:在运行以下代码之前,创建了两个文件,如下所示:

file.txt

gfg.txt

// C# program to illustrate the usage 
// of File.GetCreationTime(String) method 
  
// Using System and System.IO namespaces 
using System; 
using System.IO; 
  
class GFG { 
    static void Main(string[] args) 
    { 
        // Calling the GetCreationTime() function 
        DateTime fileCreatedDate1 = File.GetCreationTime(@"file.txt"); 
        DateTime fileCreatedDate2 = File.GetCreationTime(@"gfg.txt"); 
  
        // Printing the creation date and time of the 
        // specified file 
        Console.WriteLine("File created on:" + fileCreatedDate1); 
        Console.WriteLine("File created on:" + fileCreatedDate2); 
    } 
}

执行中:

File created on:4/19/2020 4:02:54 AM
File created on:4/19/2020 4:07:02 AM



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 File.GetCreationTime() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。