File.SetLastWriteTime 方法

定义

重载

SetLastWriteTime(SafeFileHandle, DateTime)

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

SetLastWriteTime(String, DateTime)

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

SetLastWriteTime(SafeFileHandle, DateTime)

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

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

lastWriteTime
DateTime

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

例外

fileHandlenull

lastWriteTime 指定一个超出该操作所允许的日期范围和/或时间范围的值。

调用方没有所要求的权限。

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

适用于

SetLastWriteTime(String, DateTime)

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 的上次写入日期和时间设置的值。 该值用本地时间表示。

例外

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

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

未找到指定路径。

调用方没有所要求的权限。

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());
        }
    }
}
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 任务

另请参阅

适用于