📜  C#中的File.GetLastAccessTimeUtc()方法与示例

📅  最后修改于: 2021-05-29 23:30:07             🧑  作者: Mango

File.GetLastAccessTimeUtc(String)是一个内置的File类方法,该方法用于以协调世界时(UTC)返回上次访问指定文件或目录的日期和时间。
句法:

public static DateTime GetLastAccessTimeUtc (string path);

参数:该函数接受如下所示的参数:

例外情况:

  • UnauthorizedAccessException:调用者没有所需的权限。
  • ArgumentException:路径是长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径为null。
  • PathTooLongException:给定的路径,文件名或两者都超过了系统定义的最大长度。
  • NotSupportedException:路径格式无效。

返回值:以协调世界时(UTC)返回上次访问指定文件或目录的日期和时间。
下面是说明File.GetLastAccessTimeUtc(String)方法的程序。
程序1:在运行下面的代码之前,将创建一个文件file.txt ,其内容如下所示

file.txt

C#
// C# program to illustrate the usage
// of File.GetLastAccessTimeUtc(String) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string myfile = @"file.txt";
  
        // Calling GetLastAccessTimeUtc() function
        DateTime dt = File.GetLastAccessTimeUtc(myfile);
  
        // Getting the last access time
        Console.WriteLine("The last access time in UTC for this file was {0}.", dt);
    }
}


C#
// C# program to illustrate the usage
// of File.GetLastAccessTimeUtc(String) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string myfile = @"file.txt";
  
        // Setting the date
        File.SetLastAccessTimeUtc(myfile, new DateTime(2020, 5, 4));
  
        // Calling GetLastAccessTimeUtc() function
        DateTime dt = File.GetLastAccessTimeUtc(myfile);
  
        // Getting the last access time
        Console.WriteLine("The last access time in UTC for this file was {0}.", dt);
    }
}


执行中:

The last access time in UTC for this file was 5/4/2020 12:00:00 AM.

程序2:在运行下面的代码之前,创建了一个文件,如下所示:

file.txt

C#

// C# program to illustrate the usage
// of File.GetLastAccessTimeUtc(String) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string myfile = @"file.txt";
  
        // Setting the date
        File.SetLastAccessTimeUtc(myfile, new DateTime(2020, 5, 4));
  
        // Calling GetLastAccessTimeUtc() function
        DateTime dt = File.GetLastAccessTimeUtc(myfile);
  
        // Getting the last access time
        Console.WriteLine("The last access time in UTC for this file was {0}.", dt);
    }
}

执行中:

The last access time in UTC for this file was 5/4/2020 12:00:00 AM.