File.SetLastWriteTime(String, DateTime) Metoda

Definicja

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

Dotyczy

Zobacz też