File.GetLastWriteTime 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
GetLastWriteTime(SafeFileHandle) |
返回指定文件或目录的最后写入日期和时间。 |
GetLastWriteTime(String) |
返回上次写入指定文件或目录的日期和时间。 |
GetLastWriteTime(SafeFileHandle)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
返回指定文件或目录的最后写入日期和时间。
public:
static DateTime GetLastWriteTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle);
public static DateTime GetLastWriteTime (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);
static member GetLastWriteTime : Microsoft.Win32.SafeHandles.SafeFileHandle -> DateTime
Public Shared Function GetLastWriteTime (fileHandle As SafeFileHandle) As DateTime
参数
- fileHandle
- SafeFileHandle
要获取上次写入日期和时间信息的文件或目录的 SafeFileHandle。
返回
DateTime 结构设置为指定文件或目录的最后写入日期和时间。 此值以本地时间表示。
例外
fileHandle
null
。
调用方没有所需的权限。
适用于
GetLastWriteTime(String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
返回上次写入指定文件或目录的日期和时间。
public:
static DateTime GetLastWriteTime(System::String ^ path);
public static DateTime GetLastWriteTime (string path);
static member GetLastWriteTime : string -> DateTime
Public Shared Function GetLastWriteTime (path As String) As DateTime
参数
- path
- String
要获取其写入日期和时间信息的文件或目录。
返回
DateTime 结构设置为上次写入指定文件或目录的日期和时间。 此值以本地时间表示。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本:path
为零长度的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 GetInvalidPathChars() 方法查询无效字符。
path
null
。
指定的路径、文件名或两者都超过了系统定义的最大长度。
path
格式无效。
示例
以下示例演示 GetLastWriteTime
。
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());
}
}
}
open System
open System.IO
let path = @"c:\Temp\MyTest.txt"
if File.Exists path |> not then
File.Create path |> ignore
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.
let dt = File.GetLastWriteTime path
printfn $"The last write time for this file was {dt}."
// Update the last write time.
File.SetLastWriteTime(path, DateTime.Now)
let dt2 = File.GetLastWriteTime path
printfn $"The last write time for this file was {dt2}."
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 some 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
参数中描述的文件不存在,此方法返回 1601 年 1 月 1 日午夜 12:00(C.E.)协调世界时(UTC),调整为当地时间。
允许 path
参数指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。