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


C# File.GetCreationTimeUtc()用法及代碼示例

File.GetCreationTimeUtc(String)是一個內置的File類方法,該方法用於以協調世界時(UTC)返回指定文件或目錄的創建日期和時間。

用法:

public static DateTime GetCreationTimeUtc (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:路徑格式無效。

返回值:以協調世界時(UTC)返回指定文件或目錄的創建日期和時間。

下麵是說明File.GetCreationTimeUtc(String)方法的程序。

程序1:在運行下麵的代碼之前,將創建一個文件file.txt,其內容如下所示:

file.txt

// C# program to illustrate the usage 
// of File.GetCreationTimeUtc(String) method 
  
// Using System and System.IO namespaces 
using System; 
using System.IO; 
  
class GFG { 
    static void Main(string[] args) 
    { 
        // Calling the GetCreationTimeUtc() function 
        DateTime fileCreatedDate = File.GetCreationTimeUtc(@"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.GetCreationTimeUtc(String) method 
  
// Using System and System.IO namespaces 
using System; 
using System.IO; 
  
class GFG { 
    static void Main(string[] args) 
    { 
        // Calling the GetCreationTimeUtc() function 
        DateTime fileCreatedDate1 = File.GetCreationTimeUtc(@"file.txt"); 
        DateTime fileCreatedDate2 = File.GetCreationTimeUtc(@"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.GetCreationTimeUtc() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。