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

📅  最后修改于: 2021-05-29 16:38:22             🧑  作者: Mango

File.SetLastAccessTimeUtc(String,DateTime)是一个内置的File类方法,该方法用于以协调世界时(UTC)设置最后一次访问指定文件的日期和时间。
句法:

public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc);

参数:该函数接受两个参数,如下所示:

例外情况:

  • ArgumentException:路径是长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径为null。
  • PathTooLongException:指定的路径,文件名或两者都超过系统定义的最大长度。
  • FileNotFoundException:找不到指定的路径
  • UnauthorizedAccessException:调用者没有所需的权限。
  • NotSupportedException:路径格式无效。
  • ArgumentOutOfRangeException: lastAccessTime指定一个超出此操作允许的日期或时间范围的值。

下面是说明File.SetLastAccessTimeUtc(String,DateTime)方法的程序。
程序1:在运行下面的代码之前,将创建一个文件file.txt ,其内容如下所示:

file.txt

C#
// C# program to illustrate the usage
// of File.SetLastAccessTimeUtc() 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 the SetLastAccessTimeUtc() function
        // to set last access date and time
        File.SetLastAccessTimeUtc(myfile, new DateTime(2020, 5, 4, 4, 5, 7));
  
        // Getting the last access date and time
        DateTime dt = File.GetLastAccessTimeUtc(myfile);
        Console.WriteLine("The last access date and "+
             "time in UTC for this file was {0}.", dt);
    }
}


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


输出:

The last access date and time in UTC for this file was 5/4/2020 4:05:07 AM.

程序2:最初没有创建文件。下面的代码本身创建了一个文件file.txt并显示了上次访问的日期和时间。

C#

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

执行中:

The last access date and time in UTC for this file was 5/4/2019 4:05:07 AM.