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

📅  最后修改于: 2021-05-29 18:34:44             🧑  作者: Mango

File.SetCreationTimeUtc(String,DateTime)是一个内置的File类方法,该方法用于以协调世界时(UTC)设置创建文件的日期和时间。
句法:

public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc);

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

例外情况:

  • FileNotFoundException:找不到指定的路径
  • ArgumentException:路径是长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径为null。
  • PathTooLongException:指定的路径,文件名或两者都超过系统定义的最大长度。
  • IOException:执行操作时发生I / O错误。
  • ArgumentOutOfRangeException:创建时间指定了此操作所允许的日期,时间或两者之外的值。
  • UnauthorizedAccessException:调用者没有所需的权限。
  • NotSupportedException:路径格式无效。

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

file.txt

C#
// C# program to illustrate the usage
// of File.SetCreationTimeUtc(String, DateTime) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    static void Main()
    {
        // Specifying a new date and time
        DateTime D1 = new DateTime(2017, 12, 25, 2, 6, 8);
  
        // Calling SetCreationTimeUtc() function
        // to set the new date and time
        File.SetCreationTimeUtc("file.txt", D1);
  
        // Calling the GetCreationTimeUtc() function
        // to get the creation date and time
        DateTime D2 = File.GetCreationTimeUtc("file.txt");
        Console.WriteLine("The File Creation Time in UTC is : " + D2.ToString());
    }
}


C#
// C# program to illustrate the usage
// of File.SetCreationTimeUtc(String, DateTime) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    static void Main()
    {
        // Calling the GetCreationTimeUtc() function to
        // get the original file creation date and time in UTC
        DateTime D1 = File.GetCreationTimeUtc("file.txt");
        Console.WriteLine("File Creation Old Time in UTC is: " + D1.ToString());
  
        // Specifying a new date and time
        DateTime D2 = new DateTime(2017, 12, 25, 2, 6, 8);
  
        // Calling SetCreationTimeUtc() function
        // to set the new date and time
        File.SetCreationTimeUtc("file.txt", D2);
  
        // Calling the GetCreationTimeUtc() function
        // to get the new creation date and time
        DateTime D3 = File.GetCreationTimeUtc("file.txt");
        Console.WriteLine("File Creation new Time in UTC is : " + D3.ToString());
    }
}


输出:

The File Creation Time in UTC is : 25/12/2017 02:06:08 AM

程序2:在运行下面的代码之前,将创建一个文件file.txt ,其内容如下所示:

file.txt

C#

// C# program to illustrate the usage
// of File.SetCreationTimeUtc(String, DateTime) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    static void Main()
    {
        // Calling the GetCreationTimeUtc() function to
        // get the original file creation date and time in UTC
        DateTime D1 = File.GetCreationTimeUtc("file.txt");
        Console.WriteLine("File Creation Old Time in UTC is: " + D1.ToString());
  
        // Specifying a new date and time
        DateTime D2 = new DateTime(2017, 12, 25, 2, 6, 8);
  
        // Calling SetCreationTimeUtc() function
        // to set the new date and time
        File.SetCreationTimeUtc("file.txt", D2);
  
        // Calling the GetCreationTimeUtc() function
        // to get the new creation date and time
        DateTime D3 = File.GetCreationTimeUtc("file.txt");
        Console.WriteLine("File Creation new Time in UTC is : " + D3.ToString());
    }
}

输出:

File Creation Old Time in UTC is: 4/25/2020 11:26:14 AM
File Creation new Time in UTC is : 25/12/2017 02:06:08 AM