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

ディレクトリのパス。

戻り値

DateTime

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

例外

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

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path 長さが 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、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 記述されているディレクトリが存在しない場合、このメソッドは 1601 年 1 月 1 日午前 0 時 00 分 (午前 12 時) を返します。 (C.E.)協定世界時 (UTC)、現地時刻に調整されます。

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

パラメーターでは path 大文字と小文字は区別されません。

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

適用対象

こちらもご覧ください