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,其內容如下所示:
// 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:在運行以下代碼之前,創建了兩個文件,如下所示:
// 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
相關用法
- C# MathF.Cos()用法及代碼示例
- C# MathF.Sin()用法及代碼示例
- C# MathF.Min()用法及代碼示例
- C# MathF.Max()用法及代碼示例
- C# MathF.Log()用法及代碼示例
- C# MathF.Abs()用法及代碼示例
- C# MathF.Exp()用法及代碼示例
- C# MathF.Pow()用法及代碼示例
- C# MathF.Tan()用法及代碼示例
- C# Int16.Equals用法及代碼示例
- C# SByte.Equals用法及代碼示例
- C# UInt16.GetHashCode用法及代碼示例
- C# SByte.GetTypeCode用法及代碼示例
- C# SByte.GetHashCode用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 File.GetCreationTime() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。