共用方式為


File.SetLastAccessTime 方法

定義

多載

SetLastAccessTime(String, DateTime)

設定上次存取指定檔案的日期和時間。

SetLastAccessTime(SafeFileHandle, DateTime)

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

SetLastAccessTime(String, DateTime)

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

設定上次存取指定檔案的日期和時間。

public:
 static void SetLastAccessTime(System::String ^ path, DateTime lastAccessTime);
public static void SetLastAccessTime (string path, DateTime lastAccessTime);
static member SetLastAccessTime : string * DateTime -> unit
Public Shared Sub SetLastAccessTime (path As String, lastAccessTime As DateTime)

參數

path
String

要設定存取日期和時間信息的檔案。

lastAccessTime
DateTime

DateTime,其中包含要針對 path的最後一個存取日期和時間所設定的值。 此值會以當地時間表示。

例外狀況

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

path null

指定的路徑、檔名或兩者都超過系統定義的最大長度。

找不到指定的路徑。

呼叫端沒有必要的許可權。

path 格式無效。

lastAccessTime 指定超出此作業允許的日期或時間範圍以外的值。

範例

下列範例會檢查文件系統是否有指定的檔案,並視需要建立它,然後設定並取得上次存取時間。

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      String^ path = "c:\\Temp\\MyTest.txt";
      if (  !File::Exists( path ) )
      {
         File::Create( path );
         
         // Update the last access time.    
      }
      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);
                // Update the last access time.	
            }
            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

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

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

    // Update the last access time.
    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 dt = File.GetLastAccessTime path
    printfn $"The last access time for this file was {dt}."
with
| e -> printfn $"The process failed: {e}"
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 參數可以指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

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

另請參閱

適用於

SetLastAccessTime(SafeFileHandle, DateTime)

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

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

public:
 static void SetLastAccessTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, DateTime lastAccessTime);
public static void SetLastAccessTime (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, DateTime lastAccessTime);
static member SetLastAccessTime : Microsoft.Win32.SafeHandles.SafeFileHandle * DateTime -> unit
Public Shared Sub SetLastAccessTime (fileHandle As SafeFileHandle, lastAccessTime As DateTime)

參數

fileHandle
SafeFileHandle

要設定上次存取日期和時間資訊的檔案或目錄 SafeFileHandle

lastAccessTime
DateTime

DateTime,其中包含要針對 fileHandle的最後一個存取日期和時間所設定的值。 此值會以當地時間表示。

例外狀況

fileHandle null

lastAccessTime 指定超出此作業允許的日期、時間或兩者範圍以外的值。

呼叫端沒有必要的許可權。

執行作業時發生 I/O 錯誤。

適用於