文件。C# 中的 SetLastWriteTimeUtc()方法,带示例

原文:https://www . geesforgeks . org/file-setlastwittimeutc-method-in-csharp-with-examples/

文件。setlastwittimeutc(String)是一个内置的 File 类方法,用于设置指定文件上次写入的日期和时间,以协调世界时(Utc)表示。 语法:

public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTime);

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

  • Path: This is the specified file for which date and time information is set.
  • Last writing time: This is the specified last writing date and time of the path, expressed in UTC time.

例外:

  • 参数异常:路径是一个零长度字符串,只包含空格或一个或多个无效字符,如 InvalidPathChars 所定义。
  • ArgumentNullException:路径为空。
  • 路径工具异常:指定的路径、文件名或两者都超过了系统定义的最大长度。
  • 文件未找到异常:未找到指定的路径
  • 未授权访问异常:调用方没有所需的权限。
  • notSupportDexception:路径的格式无效。
  • ArgumentOutOfRangeException:最后写入时间指定的值超出了此操作允许的日期或时间范围。

下面是说明文件的程序。方法。 程序 1: 在运行下面的代码之前,创建了一个文件 file.txt ,内容如下所示:

file.txt

C

// C# program to illustrate the usage
// of File.SetLastWriteTimeUtc() 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 SetLastWriteTimeUtc() function
        // to set last written date and time in UTC
        File.SetLastWriteTimeUtc(myfile, new DateTime(2020,
                                           5, 4, 4, 5, 7));

        // Getting the last written date and time
        // expressed in UTC of the file
        DateTime dt = File.GetLastWriteTimeUtc(myfile);
        Console.WriteLine("The last written date and"+
           " time in UTC for this file was {0}.", dt);
    }
}

输出:

The last written 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.SetLastWriteTimeUtc() 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 SetLastWriteTimeUtc() function
        // to set last written date and time in UTC
        File.SetLastWriteTimeUtc(path, new DateTime(2019,
                                         5, 4, 4, 5, 7));

        // Getting the last written date and time in UTC
        // of the specified file
        DateTime dt = File.GetLastWriteTimeUtc(path);
        Console.WriteLine("The last written date and time "+
                       "in UTC for this file was {0}.", dt);
    }
}

执行:

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