File.SetLastWriteTime(String, DateTime) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
设置上次写入指定文件的日期和时间。
public:
static void SetLastWriteTime(System::String ^ path, DateTime lastWriteTime);
public static void SetLastWriteTime (string path, DateTime lastWriteTime);
static member SetLastWriteTime : string * DateTime -> unit
Public Shared Sub SetLastWriteTime (path As String, lastWriteTime As DateTime)
参数
- path
- String
要设置其日期和时间信息的文件。
例外
.NET Framework 和 .net Core 版本早于2.1: path
是一个长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
未找到指定路径。
调用方没有所要求的权限。
path
的格式无效。
lastWriteTime
指定一个超出该操作允许的日期或时间范围的值。
示例
下面的示例检查文件系统中是否存在指定的文件,并根据需要创建文件,然后设置并获取该文件的上次写入时间。
using namespace System;
using namespace System::IO;
int main()
{
try
{
String^ path = "c:\\Temp\\MyTest.txt";
if ( !File::Exists( path ) )
{
File::Create( path );
}
else
{
// Take an action that will affect the write time.
File::SetLastWriteTime( path, DateTime(1985,4,3) );
}
// Get the creation time of a well-known directory.
DateTime dt = File::GetLastWriteTime( path );
Console::WriteLine( "The last write time for this file was {0}.", dt );
// Update the last write time.
File::SetLastWriteTime( path, DateTime::Now );
dt = File::GetLastWriteTime( path );
Console::WriteLine( "The last write time for this file was {0}.", dt );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
string path = @"c:\Temp\MyTest.txt";
if (!File.Exists(path))
{
File.Create(path);
}
else
{
// Take an action that will affect the write time.
File.SetLastWriteTime(path, new DateTime(1985,4,3));
}
// Get the creation time of a well-known directory.
DateTime dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write time for this file was {0}.", dt);
// Update the last write time.
File.SetLastWriteTime(path, DateTime.Now);
dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write time for this file was {0}.", dt);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Try
Dim path As String = "c:\Temp\MyTest.txt"
If File.Exists(path) = False Then
File.Create(path)
Else
' Take an action that will affect the write time.
File.SetLastWriteTime(path, New DateTime(1985, 4, 3))
End If
' Get the creation time of a well-known directory.
Dim dt As DateTime = File.GetLastWriteTime(path)
Console.WriteLine("The last write time for this file was {0}.", dt)
' Update the last write time.
File.SetLastWriteTime(path, DateTime.Now)
dt = File.GetLastWriteTime(path)
Console.WriteLine("The last write time for this file was {0}.", dt)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
注解
path
允许参数指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参见 GetCurrentDirectory 。
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。