Directory.GetLastAccessTimeUtc(String) メソッド

定義

指定されたファイルまたはディレクトリが最後にアクセスされた日付と時刻を世界協定時刻 (UTC) 形式で返します。

public static DateTime GetLastAccessTimeUtc (string path);

パラメーター

path
String

アクセス日時情報を取得する対象のファイルまたはディレクトリ。

戻り値

指定されたファイルまたはディレクトリが最後にアクセスされた日時に設定された構造体。 この値は UTC 時刻で表記されます。

例外

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

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

pathnullです。

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

path パラメーターの形式が正しくありません。

次の例は、協定世界時 (UTC) 出力を使用する場合の出力の違いを示しています。

// This sample shows the differences between dates from methods that use
//coordinated universal time (UTC) format and those that do not.
using System;
using System.IO;

namespace IOSamples
{
  public class DirectoryUTCTime
  {
    public static void Main()
    {
    // Set the directory.
      string n = @"C:\test\newdir";
        //Create two variables to use to set the time.
      DateTime dtime1 = new DateTime(2002, 1, 3);
      DateTime dtime2 = new DateTime(1999, 1, 1);

    //Create the directory.
      try
      {
          Directory.CreateDirectory(n);
      }
      catch (IOException e)
      {
          Console.WriteLine(e);
      }

    //Set the creation and last access times to a variable DateTime value.
      Directory.SetCreationTime(n, dtime1);
      Directory.SetLastAccessTimeUtc(n, dtime1);

        // Print to console the results.
      Console.WriteLine("Creation Date: {0}", Directory.GetCreationTime(n));
      Console.WriteLine("UTC creation Date: {0}", Directory.GetCreationTimeUtc(n));
      Console.WriteLine("Last write time: {0}", Directory.GetLastWriteTime(n));
      Console.WriteLine("UTC last write time: {0}", Directory.GetLastWriteTimeUtc(n));
      Console.WriteLine("Last access time: {0}", Directory.GetLastAccessTime(n));
      Console.WriteLine("UTC last access time: {0}", Directory.GetLastAccessTimeUtc(n));

        //Set the last write time to a different value.
      Directory.SetLastWriteTimeUtc(n, dtime2);
      Console.WriteLine("Changed last write time: {0}", Directory.GetLastWriteTimeUtc(n));
    }
  }
}
// Obviously, since this sample deals with dates and times, the output will vary
// depending on when you run the executable. Here is one example of the output:
//Creation Date: 1/3/2002 12:00:00 AM
//UTC creation Date: 1/3/2002 8:00:00 AM
//Last write time: 12/31/1998 4:00:00 PM
//UTC last write time: 1/1/1999 12:00:00 AM
//Last access time: 1/2/2002 4:00:00 PM
//UTC last access time: 1/3/2002 12:00:00 AM
//Changed last write time: 1/1/1999 12:00:00 AM

注釈

注意

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

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

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

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください