File.SetLastWriteTime 方法
设置上次写入指定文件的日期和时间。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Sub SetLastWriteTime ( _
path As String, _
lastWriteTime As DateTime _
)
用法
Dim path As String
Dim lastWriteTime As DateTime
File.SetLastWriteTime(path, lastWriteTime)
public static void SetLastWriteTime (
string path,
DateTime lastWriteTime
)
public:
static void SetLastWriteTime (
String^ path,
DateTime lastWriteTime
)
public static void SetLastWriteTime (
String path,
DateTime lastWriteTime
)
public static function SetLastWriteTime (
path : String,
lastWriteTime : DateTime
)
参数
- path
要设置其日期和时间信息的文件。
- lastWriteTime
DateTime,它包含要为 path 的上次写入日期和时间设置的值。该值用本地时间表示。
异常
异常类型 | 条件 |
---|---|
path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。 |
|
path 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。 |
|
未找到指定的路径。 |
|
调用方没有所要求的权限。 |
|
path 的格式无效。 |
|
lastWriteTime 指定超出该操作允许的日期或时间范围的值。 |
备注
允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。
有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
重命名或移动文件。 |
示例
下面的示例检查文件系统中是否有指定的文件,如需要则创建该文件,然后设置并获取文件的上次写入时间。
Imports System
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
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());
}
}
}
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 );
}
}
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
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.get_Now());
dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write time for this file was {0}.",
dt);
}
catch (System.Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
} //main
} //Test
.NET Framework 安全性
- FileIOPermission 用于写入到指定的文件。关联的枚举:FileIOPermissionAccess.Write
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0