共用方式為


Directory.SetLastAccessTime(String, DateTime) 方法

定義

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

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

包含值以設定 path 存取日期和時間的物件。 這個值以本地時間表示。

例外狀況

找不到指定的路徑。

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

pathnull

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

呼叫端沒有必要的權限。

目前的作業系統不是 Windows NT 或更新版本。

lastAccessTime 指定的值超出這項作業允許的日期或時間範圍。

範例

下列範例示範如何使用 SetLastAccessTime

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 last access 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 last access 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 last access 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 last 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

備註

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

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

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

適用於

另請參閱