File.SetLastWriteTime 方法

定义

重载

名称 说明
SetLastWriteTime(SafeFileHandle, DateTime)

设置指定文件或目录上次写入到的日期和时间。

SetLastWriteTime(String, DateTime)

设置指定文件上次写入到的日期和时间。

SetLastWriteTime(SafeFileHandle, DateTime)

Source:
File.cs
Source:
File.cs
Source:
File.cs
Source:
File.cs
Source:
File.cs

设置指定文件或目录上次写入到的日期和时间。

public:
 static void SetLastWriteTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, DateTime lastWriteTime);
public static void SetLastWriteTime(Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, DateTime lastWriteTime);
static member SetLastWriteTime : Microsoft.Win32.SafeHandles.SafeFileHandle * DateTime -> unit
Public Shared Sub SetLastWriteTime (fileHandle As SafeFileHandle, lastWriteTime As DateTime)

参数

fileHandle
SafeFileHandle

要为其设置上次写入日期和时间信息的文件或目录的 A SafeFileHandle

lastWriteTime
DateTime

一个 DateTime 包含要为上次写入日期和时间 fileHandle设置的值。 此值以本地时间表示。

例外

fileHandlenull

lastWriteTime 指定超出此作允许的日期、时间或两者范围之外的值。

调用方没有所需的权限。

执行操作时发生 I/O 错误。

适用于

SetLastWriteTime(String, DateTime)

Source:
File.cs
Source:
File.cs
Source:
File.cs
Source:
File.cs
Source:
File.cs

设置指定文件上次写入到的日期和时间。

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

要为其设置日期和时间信息的文件。

lastWriteTime
DateTime

一个 DateTime 包含要为上次写入日期和时间 path设置的值。 此值以本地时间表示。

例外

低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。

pathnull

指定的路径、文件名或两者都超过了系统定义的最大长度。

找不到指定的路径。

调用方没有所需的权限。

path 格式无效。

lastWriteTime 指定超出此操作允许的日期或时间范围的值。

示例

以下示例检查文件系统中是否有指定文件、根据需要创建文件,然后设置并获取文件的最后一次写入时间。

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

try
    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 dt = File.GetLastWriteTime path
    printfn $"The last write time for this file was {dt}."
with
| e -> printfn $"The process failed: {e}"
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 任务

另请参阅

适用于