File.SetLastWriteTime Metoda

Definicja

Przeciążenia

SetLastWriteTime(SafeFileHandle, DateTime)

Ustawia datę i godzinę ostatniego zapisania określonego pliku lub katalogu.

SetLastWriteTime(String, DateTime)

Ustawia datę i godzinę ostatniego zapisania określonego pliku.

SetLastWriteTime(SafeFileHandle, DateTime)

Źródło:
File.cs
Źródło:
File.cs
Źródło:
File.cs

Ustawia datę i godzinę ostatniego zapisania określonego pliku lub katalogu.

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)

Parametry

fileHandle
SafeFileHandle

A SafeFileHandle do pliku lub katalogu, dla którego należy ustawić ostatnią datę i godzinę zapisu.

lastWriteTime
DateTime

Wartość DateTime zawierająca wartość ustawioną dla daty i godziny ostatniego zapisu .fileHandle Ta wartość jest wyrażona w czasie lokalnym.

Wyjątki

fileHandle to null.

lastWriteTime Określa wartość poza zakresem dat, godzin lub obu dozwolonych dla tej operacji.

Obiekt wywołujący nie posiada wymaganych uprawnień.

Wystąpił błąd we/wy podczas wykonywania operacji.

Dotyczy

SetLastWriteTime(String, DateTime)

Źródło:
File.cs
Źródło:
File.cs
Źródło:
File.cs

Ustawia datę i godzinę ostatniego zapisania określonego pliku.

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)

Parametry

path
String

Plik, dla którego chcesz ustawić informacje o dacie i godzinie.

lastWriteTime
DateTime

Wartość DateTime zawierająca wartość ustawioną dla daty i godziny ostatniego zapisu .path Ta wartość jest wyrażona w czasie lokalnym.

Wyjątki

.NET Framework i .NET Core w wersjach starszych niż 2.1: path jest ciągiem o zerowej długości, zawiera tylko biały odstęp lub zawiera co najmniej jeden nieprawidłowy znak. Możesz wykonać zapytanie o nieprawidłowe znaki przy użyciu GetInvalidPathChars() metody .

path to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Nie można odnaleźć określonej ścieżki.

Obiekt wywołujący nie posiada wymaganych uprawnień.

path jest w nieprawidłowym formacie.

lastWriteTime określa wartość poza zakresem dat lub godzin dozwolonych dla tej operacji.

Przykłady

Poniższy przykład sprawdza system plików dla określonego pliku, tworząc plik w razie potrzeby, a następnie ustawia i pobiera ostatni czas zapisu pliku.

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

Uwagi

Parametr path może określać informacje o ścieżce względnej lub bezwzględnej. Informacje o ścieżce względnej są interpretowane jako względne w stosunku do bieżącego katalogu roboczego. Aby uzyskać bieżący katalog roboczy, zobacz GetCurrentDirectory.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Zobacz też

Dotyczy