共用方式為


Directory.GetCreationTime(String) 方法

定義

取得目錄的建立日期和時間。

public:
 static DateTime GetCreationTime(System::String ^ path);
public static DateTime GetCreationTime (string path);
static member GetCreationTime : string -> DateTime
Public Shared Function GetCreationTime (path As String) As DateTime

參數

path
String

目錄的路徑。

傳回

結構,設定為指定之目錄的建立日期和時間。 這個值以本地時間表示。

例外狀況

呼叫端沒有必要的權限。

.NET Framework 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

範例

下列範例會取得指定目錄的建立時間。

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      
      // Get the creation time of a well-known directory.
      DateTime dt = Directory::GetCreationTime( Environment::CurrentDirectory );
      
      // Give feedback to the user.
      if ( DateTime::Now.Subtract( dt ).TotalDays > 364 )
      {
         Console::WriteLine( "This directory is over a year old." );
      }
      else
      if ( DateTime::Now.Subtract( dt ).TotalDays > 30 )
      {
         Console::WriteLine( "This directory is over a month old." );
      }
      else
      if ( DateTime::Now.Subtract( dt ).TotalDays <= 1 )
      {
         Console::WriteLine( "This directory is less than a day old." );
      }
      else
      {
         Console::WriteLine( "This directory was created on {0}", dt );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Get the creation time of a well-known directory.
            DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);

            // Give feedback to the user.
            if (DateTime.Now.Subtract(dt).TotalDays > 364)
            {
                Console.WriteLine("This directory is over a year old.");
            }
            else if (DateTime.Now.Subtract(dt).TotalDays > 30)
            {
                Console.WriteLine("This directory is over a month old.");
            }
            else if (DateTime.Now.Subtract(dt).TotalDays <= 1)
            {
                Console.WriteLine("This directory is less than a day old.");
            }
            else
            {
                Console.WriteLine("This directory was created on {0}", dt);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
open System
open System.IO

try
    // Get the creation time of a well-known directory.
    let dt = Directory.GetCreationTime Environment.CurrentDirectory

    // Give feedback to the user.
    if DateTime.Now.Subtract(dt).TotalDays > 364 then
        printfn "This directory is over a year old."
    elif DateTime.Now.Subtract(dt).TotalDays > 30 then
        printfn "This directory is over a month old."
    elif DateTime.Now.Subtract(dt).TotalDays <= 1 then
        printfn "This directory is less than a day old."
    else
        printfn $"This directory was created on {dt}"
with e ->
    printfn $"The process failed: {e}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            ' Get the creation time of a well-known directory.
            Dim dt As DateTime = Directory.GetCreationTime(Environment.CurrentDirectory)

            ' Give feedback to the user.
            If DateTime.Now.Subtract(dt).TotalDays > 364 Then
                Console.WriteLine("This directory is over a year old.")
            ElseIf DateTime.Now.Subtract(dt).TotalDays > 30 Then
                Console.WriteLine("This directory is over a month old.")
            ElseIf DateTime.Now.Subtract(dt).TotalDays <= 1 Then
                Console.WriteLine("This directory is less than a day old.")
            Else
                Console.WriteLine("This directory was created on {0}", dt)
            End If

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

備註

注意

這個方法可能會傳回不正確的值,因為它會使用操作系統可能不會持續更新其值的原生函式。

這個方法相當於 File.GetCreationTime

如果 參數中所述的 path 目錄不存在,此方法會傳回 12:00 午夜,1601 A.D. (C.E.) 國際標準時間 (UTC) ,調整為當地時間。

允許 path 參數指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

參數的 path 區分大小寫會對應至程式代碼執行所在的文件系統。 例如,在NTFS上不區分大小寫 (預設Windows檔案系統) 和Linux檔案系統上區分大小寫。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

適用於

另請參閱