共用方式為


File.GetLastAccessTime 方法

定義

多載

GetLastAccessTime(SafeFileHandle)

傳回指定之檔案或目錄的最後存取日期和時間。

GetLastAccessTime(String)

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

GetLastAccessTime(SafeFileHandle)

來源:
File.cs
來源:
File.cs
來源:
File.cs

傳回指定之檔案或目錄的最後存取日期和時間。

public:
 static DateTime GetLastAccessTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle);
public static DateTime GetLastAccessTime (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);
static member GetLastAccessTime : Microsoft.Win32.SafeHandles.SafeFileHandle -> DateTime
Public Shared Function GetLastAccessTime (fileHandle As SafeFileHandle) As DateTime

參數

fileHandle
SafeFileHandle

SafeFileHandle 取得上次存取日期與時間資訊的檔案或目錄的 。

傳回

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

例外狀況

fileHandlenull

呼叫端沒有必要的權限。

適用於

GetLastAccessTime(String)

來源:
File.cs
來源:
File.cs
來源:
File.cs

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

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

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

傳回

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

例外狀況

呼叫端沒有必要的權限。

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

pathnull

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

path 格式無效。

範例

下列範例示範 GetLastAccessTime

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      String^ path = "c:\\Temp\\MyTest.txt";
      if (  !File::Exists( path ) )
      {
         File::Create( path );
      }
      File::SetLastAccessTime( path, DateTime(1985,5,4) );
      
      // Get the creation time of a well-known directory.
      DateTime dt = File::GetLastAccessTime( path );
      Console::WriteLine( "The last access time for this file was {0}.", dt );
      
      // Update the last access time.
      File::SetLastAccessTime( path, DateTime::Now );
      dt = File::GetLastAccessTime( path );
      Console::WriteLine( "The last access time for this file 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:\Temp\MyTest.txt";

            if (!File.Exists(path))
            {
                File.Create(path);
            }
            File.SetLastAccessTime(path, new DateTime(1985,5,4));

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

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

let path = @"c:\Temp\MyTest.txt"

if File.Exists path |> not then
    File.Create path |> ignore

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

// Get the creation time of a well-known directory.
let dt = File.GetLastAccessTime path
printfn $"The last access time for this file was {dt}."

// Update the last access time.
File.SetLastAccessTime(path, DateTime.Now)
let dt2 = File.GetLastAccessTime path
printfn $"The last access time for this file was {dt2}."
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Try
            Dim path As String = "c:\Temp\MyTest.txt"
            If File.Exists(path) = False Then
                File.Create(path)
            End If
            File.SetLastAccessTime(path, New DateTime(1985, 5, 4))

            ' Get the creation time of a well-known directory.
            Dim dt As DateTime = File.GetLastAccessTime(path)

            Console.WriteLine("The last access time for this file was {0}.", dt)

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

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

備註

注意

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

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

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

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

另請參閱

適用於