FileSystemInfo.LastAccessTime プロパティ

定義

現在のファイルまたはディレクトリに最後にアクセスした時刻を取得または設定します。

public:
 property DateTime LastAccessTime { DateTime get(); void set(DateTime value); };
public DateTime LastAccessTime { get; set; }
member this.LastAccessTime : DateTime with get, set
Public Property LastAccessTime As DateTime

プロパティ値

現在のファイルまたはディレクトリに最後にアクセスした時刻。

例外

Refresh() がデータを初期化できません。

現在のオペレーティング システムは Windows NT 以降ではありません。

呼び出し元が、無効なアクセス時間を設定しようとしています。

次のコード例では、"タッチ" 操作を使用して LastAccessTime プロパティを更新する方法を示します。 この例では、ファイルは "touched" で、 プロパティと LastWriteTime プロパティを LastAccessTimeCreationTime現在の日付と時刻に更新します。

using System;
using System.IO;

namespace touch
{
    class Touch
    {
        static void Main(string[] args)
        {
            // Make sure a filename was provided.
            if (args.Length > 0)
            {
                // Verify that the provided filename exists.
                if (File.Exists(args[0]))
                {
                    FileInfo fi = new FileInfo(args[0]);
                    touchFile(fi);
                }
                else
                {
                    Console.WriteLine(
                        "Could not find the file: {0}.", args[0]);
                }
            }
            else
            {
                Console.WriteLine("No file was specified.");
            }
        }

        static void touchFile(FileSystemInfo fsi)
        {
            Console.WriteLine("Touching: {0}", fsi.FullName);

            // Update the CreationTime, LastWriteTime and LastAccessTime.
            try
            {
                fsi.CreationTime = fsi.LastWriteTime = fsi.LastAccessTime =
                    DateTime.Now;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
    }
}
Imports System.IO

Public Class Touch
    Public Shared Sub Main(ByVal args() As String)

        ' Make sure an argument (filename) was provided.
        If args.Length > 0 Then

            ' Verify that the provided filename exists.
            If File.Exists(args(0)) Then
                Dim fi As FileInfo = New FileInfo(args(0))
                touchFile(fi)
            Else
                Console.WriteLine("Could not find the file {0}", args(0))
            End If
        Else
            Console.WriteLine("No file specified.")
        End If
    End Sub

    Public Shared Sub touchFile(ByVal fsi As FileSystemInfo)
        Console.WriteLine("Touching: {0}", fsi.FullName)

        ' Update the CreationTime, LastWriteTime and LastAccessTime.
        Try
            fsi.CreationTime = DateTime.Now
            fsi.LastAccessTime = DateTime.Now
            fsi.LastWriteTime = DateTime.Now
        Catch e As Exception
            Console.WriteLine("Error: {0}", e.Message)
        End Try

    End Sub

End Class

注釈

Note

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

オブジェクトに FileSystemInfo 記述されているファイルが存在しない場合、このプロパティは 1601 年 1 月 1 日午前 0 時 00 分 (C.E.) を返します。協定世界時 (UTC) で、現地時刻に調整されます。

オブジェクトの現在のLastAccessTimeUtcインスタンスが次DirectoryInfoのいずれかのメソッドから返された場合、FileSystemInfoプロパティの値は事前にキャッシュされます。

最新の値を取得するには、 メソッドを呼び出します Refresh

適用対象

こちらもご覧ください