共用方式為


Directory.GetLastAccessTime(String) 方法

定義

傳回指定檔案或目錄上次被存取的日期和時間。

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

參數

path
String

要取得其存取日期和時間資訊的檔案或目錄。

傳回

結構,設定為指定之檔案或目錄上次被存取的日期和時間。 這個值以本地時間表示。

例外狀況

呼叫端沒有必要的權限。

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

pathnull

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

path 參數的格式無效。

範例

下列範例示範如何使用 GetLastAccessTime

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      String^ path = "c:\\MyDir";
      if (  !Directory::Exists( path ) )
      {
         Directory::CreateDirectory( path );
      }
      Directory::SetLastAccessTime( path, DateTime(1985,5,4) );
      
      // Get the creation time of a well-known directory.
      DateTime dt = Directory::GetLastAccessTime( path );
      Console::WriteLine( "The last access time for this directory was {0}", dt );
      
      // Update the last access time.
      Directory::SetLastAccessTime( path, DateTime::Now );
      dt = Directory::GetLastAccessTime( path );
      Console::WriteLine( "The last access time for this directory was {0}", dt );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            string path = @"c:\MyDir";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            Directory.SetLastAccessTime(path, new DateTime(1985,5,4));

            // Get the creation time of a well-known directory.
            DateTime dt = Directory.GetLastAccessTime(path);
            Console.WriteLine("The last access time for this directory was {0}", dt);
            
            // Update the last access time.
            Directory.SetLastAccessTime(path, DateTime.Now);
            dt = Directory.GetLastAccessTime(path);
            Console.WriteLine("The last access time for this directory was {0}", dt);
        }

        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
open System
open System.IO

try
    let path = @"c:\MyDir"
    if not (Directory.Exists path) then
        Directory.CreateDirectory path |> ignore

    Directory.SetLastAccessTime(path, DateTime(1985, 5, 4))

    // Get the creation time of a well-known directory.
    let dt = Directory.GetLastAccessTime path
    printfn $"The last access time for this directory was {dt}"
    
    // Update the last access time.
    Directory.SetLastAccessTime(path, DateTime.Now)
    let dt = Directory.GetLastAccessTime path
    printfn $"The last access time for this directory was {dt}"

with e ->
    printfn $"The process failed: {e}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            Dim path As String = "c:\MyDir"
            If Directory.Exists(path) = False Then
                Directory.CreateDirectory(path)
            End If
            Directory.SetLastAccessTime(path, New DateTime(1985, 5, 4))

            'Get the access time of a well-known directory.
            Dim dt As DateTime = Directory.GetLastAccessTime(path)
            Console.WriteLine("The last access time for this directory was {0}", dt)

            'Update the last access time.
            Directory.SetLastAccessTime(path, DateTime.Now)
            dt = Directory.GetLastAccessTime(path)
            Console.WriteLine("The last access time for this directory was {0}", dt)

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

備註

注意

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

這個方法與 File.GetLastAccessTime相同。

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

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

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

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

適用於

另請參閱