File.SetLastWriteTimeUtc(String)是一个内置的File类方法,该方法用于设置指定文件最后写入的日期和时间(以协调世界时(UTC))。
用法:
public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTime);
参数:此函数接受两个参数,如下所示:
- path: This is the specified file for which to set the date and time information.
- lastWriteTime: This is the specified last written date and time of the path that is expressed in UTC time.
异常:
- ArgumentException:路径为长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
- ArgumentNullException:路径为空。
- PathTooLongException:指定的路径,文件名或两者都超过了system-defined的最大长度。
- FileNotFoundException:找不到指定的路径。
- UnauthorizedAccessException:调用者没有所需的权限。
- NotSupportedException:路径格式无效。
- ArgumentOutOfRangeException:lastWriteTime指定一个超出此操作允许的日期或时间范围的值。
下面是说明File.SetLastWriteTimeUtc(String,DateTime)方法的程序。
程序1:在运行下面的代码之前,将创建一个文件file.txt,其内容如下所示:
// 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()
{
// Specifing 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# 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()
{
// Specifing 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.
相关用法
- C# MathF.Cos()用法及代码示例
- C# MathF.Sin()用法及代码示例
- C# MathF.Min()用法及代码示例
- C# MathF.Max()用法及代码示例
- C# MathF.Log()用法及代码示例
- C# MathF.Abs()用法及代码示例
- C# MathF.Exp()用法及代码示例
- C# MathF.Pow()用法及代码示例
- C# MathF.Tan()用法及代码示例
- C# Int16.Equals用法及代码示例
- C# SByte.Equals用法及代码示例
- C# UInt16.GetHashCode用法及代码示例
- C# SByte.GetTypeCode用法及代码示例
- C# SByte.GetHashCode用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 File.SetLastWriteTimeUtc() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。