Directory.GetLastAccessTime(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回上次访问指定文件或目录的日期和时间。
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() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
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
的目录不存在,此方法返回 1601 年 1 月 1 日午夜 12:00 (C.E.) 协调世界时 (UTC) 调整为本地时间。
允许 path
参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path
区分大小写对应于运行代码的文件系统的区分大小写。 例如,它在 NTFS 上不区分大小写, (默认 Windows 文件系统) ,Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。