System.IO.Directory.SetLastWriteTimeUtc 方法

方法描述

设置上次写入某个目录的日期和时间,其格式为协调世界时 (UTC)。

语法定义(C# System.IO.Directory.SetLastWriteTimeUtc 方法 的用法)

public static void SetLastWriteTimeUtc(
	string path,
	DateTime lastWriteTimeUtc
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
path System-String 目录的路径。
lastWriteTimeUtc System-DateTime 上次写入目录的日期和时间。该值用 UTC 时间表示。
返回值 void

提示和注释

允许 path 参数指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参见 GetCurrentDirectory。

path 参数不区分大小写。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

平台说明:这些操作系统不支持此方法。

System.IO.Directory.SetLastWriteTimeUtc 方法例子

下面的代码示例演示了使用协调通用时间 (UTC) 输出时,输出内容的变化。

// This sample shows the differences between dates from methods that use
//coordinated universal time (UTC) format and those that do not.
using System;
using System.IO;

namespace IOSamples
{
  public class DirectoryUTCTime
  {
    public static void Main()
    {
	// Set the directory.
      string n = @"C:\test\newdir";
		//Create two variables to use to set the time.
	  DateTime dtime1 = new DateTime(2002, 1, 3);
	  DateTime dtime2 = new DateTime(1999, 1, 1);

	//Create the directory.
	  try
	  {
		  Directory.CreateDirectory(n);
	  }
	  catch (IOException e)
	  {
		  Console.WriteLine(e);
	  }

	//Set the creation and last access times to a variable DateTime value.
	  Directory.SetCreationTime(n, dtime1);
	  Directory.SetLastAccessTimeUtc(n, dtime1);

		// Print to console the results.
	  Console.WriteLine("Creation Date: {0}", Directory.GetCreationTime(n));
	  Console.WriteLine("UTC creation Date: {0}", Directory.GetCreationTimeUtc(n));
	  Console.WriteLine("Last write time: {0}", Directory.GetLastWriteTime(n));
	  Console.WriteLine("UTC last write time: {0}", Directory.GetLastWriteTimeUtc(n));
	  Console.WriteLine("Last access time: {0}", Directory.GetLastAccessTime(n));
	  Console.WriteLine("UTC last access time: {0}", Directory.GetLastAccessTimeUtc(n));

		//Set the last write time to a different value.
      Directory.SetLastWriteTimeUtc(n, dtime2);
	  Console.WriteLine("Changed last write time: {0}", Directory.GetLastWriteTimeUtc(n));
    }
  }
}
// Obviously, since this sample deals with dates and times, the output will vary
// depending on when you run the executable. Here is one example of the output:
//Creation Date: 1/3/2002 12:00:00 AM
//UTC creation Date: 1/3/2002 8:00:00 AM
//Last write time: 12/31/1998 4:00:00 PM
//UTC last write time: 1/1/1999 12:00:00 AM
//Last access time: 1/2/2002 4:00:00 PM
//UTC last access time: 1/3/2002 12:00:00 AM
//Changed last write time: 1/1/1999 12:00:00 AM

异常

异常 异常描述
FileNotFoundException 未找到指定的路径。
ArgumentException path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。
ArgumentNullException path 为 null。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。
UnauthorizedAccessException 调用方没有所要求的权限。
PlatformNotSupportedException 当前操作系统不是 Windows NT 或更高版本。
ArgumentOutOfRangeException lastWriteTimeUtc 指定超出该操作允许的日期或时间范围的值。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。