【问题标题】:Is there a method that shows that a file was "last accessed" as opposed to "last modified", in C# or are they the same?有没有一种方法可以显示文件是“最后一次访问”而不是“最后一次修改”,在 C# 中还是相同?
【发布时间】:2019-08-27 18:08:17
【问题描述】:

我想检查文件是上次访问还是上次修改。我有在 MSDN 上检查“上次修改”信息所需的资源,但不确定“上次访问”是否是一件事。我的印象是上次修改意味着某些内容已写入文件/文件夹,而不是被访问和时间戳。

我主要是在谷歌上搜索,我根本没有尝试过任何编码,在我深入研究编写代码之前,它更多的是作为信息资源。

【问题讨论】:

  • 请标记一种编程语言。

标签: c# last-modified


【解决方案1】:

假设:

检查文件是否最后被访问过

表示获取文件最后被访问的时间。

使用File.GetLastAccessTime(path) 我能够获得最后一次访问文件的时间。这使用 system.io 命名空间。

例子:

static void Main(string[] args)
{
    try
    {
        string path = @"C:\test.txt";

        if (!File.Exists(path))
        {
            File.Create(path);
        }
        //get original access time
        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);
        Console.Read();
    }

    catch (Exception e)
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    }
}

示例来自:File.GetLastAccessTime(String) Method

【讨论】:

    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多