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

ディレクトリのパス。

返品

指定したディレクトリの作成日時に設定される構造体。 この値は現地時刻で表されます。

例外

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

次の例では、指定したディレクトリの作成時刻を取得します。

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

注釈

Note

このメソッドは、オペレーティング システムによって値が継続的に更新されないネイティブ関数を使用するため、不正確な値を返す可能性があります。

このメソッドは、 File.GetCreationTimeと同じです。

path パラメーターに記述されているディレクトリが存在しない場合、このメソッドは 1601 年 1 月 1 日午前 12 時 00 分 (C.E.) を返します。協定世界時 (UTC)、現地時刻に調整されます。

path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

適用対象

こちらもご覧ください