Directory.SetLastWriteTime(String, DateTime) Metoda

Definicja

Ustawia datę i godzinę ostatniego zapisania katalogu.

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

Ścieżka katalogu.

lastWriteTime
DateTime

Data i godzina ostatniego zapisania katalogu. Ta wartość jest wyrażona w czasie lokalnym.

Wyjątki

path nie można odnaleźć (na przykład katalog nie istnieje lub znajduje się na niezamapowanym dysku).

path nie można odnaleźć (na przykład katalog nie istnieje lub znajduje się na niezamapowanym dysku).

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

path to null.

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

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

Bieżący system operacyjny nie jest systemem Windows NT lub nowszym.

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

Przykłady

W poniższym przykładzie pokazano, jak używać polecenia SetLastWriteTime.

using namespace System;
using namespace System::IO;
void main()
{
   try
   {
      String^ path = "c:\\MyDir";
      if (  !Directory::Exists( path ) )
      {
         Directory::CreateDirectory( path );
      }
      else
      {
         
         // Take an action that will affect the write time.
         Directory::SetLastWriteTime( path, DateTime(1985,4,3) );
      }
      
      // Get the last write time of a well-known directory.
      DateTime dt = Directory::GetLastWriteTime( path );
      Console::WriteLine( "The last write time for this directory was {0}", dt );
      
      //Update the last write time.
      Directory::SetLastWriteTime( path, DateTime::Now );
      dt = Directory::GetLastWriteTime( path );
      Console::WriteLine( "The last write time for this directory 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:\MyDir";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            else
            {
                // Take an action that will affect the write time.
                Directory.SetLastWriteTime(path, new DateTime(1985,4,3));
            }

            // Get the last write time of a well-known directory.
            DateTime dt = Directory.GetLastWriteTime(path);
            Console.WriteLine("The last write time for this directory was {0}", dt);
            
            //Update the last write time.
            Directory.SetLastWriteTime(path, DateTime.Now);
            dt = Directory.GetLastWriteTime(path);
            Console.WriteLine("The last write time for this directory was {0}", dt);
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
open System
open System.IO

try
    let path = @"c:\MyDir"
    if not (Directory.Exists path) then
        Directory.CreateDirectory path |> ignore

    else
        // Take an action that will affect the write time.
        Directory.SetLastWriteTime(path, DateTime(1985, 4, 3))

    // Get the last write time of a well-known directory.
    let dt = Directory.GetLastWriteTime path
    printfn $"The last write time for this directory was {dt}"
    
    //Update the last write time.
    Directory.SetLastWriteTime(path, DateTime.Now)
    let dt = Directory.GetLastWriteTime path
    printfn $"The last write time for this directory was {dt}"
with e ->
    printfn $"The process failed: {e}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            Dim path As String = "c:\MyDir"
            If Directory.Exists(path) = False Then
                Directory.CreateDirectory(path)
            Else
                ' Take an action that will affect the write time.
                Directory.SetLastWriteTime(path, New DateTime(1985, 4, 3))
            End If

            'Get the last write time of a well-known directory.
            Dim dt As DateTime = Directory.GetLastWriteTime(path)
            Console.WriteLine("The last write time for this directory was {0}", dt)

            'Update the last write time.
            Directory.SetLastWriteTime(path, DateTime.Now)
            dt = Directory.GetLastWriteTime(path)
            Console.WriteLine("The last write time for this directory 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.

Wielkość liter parametru path odpowiada wielkości liter w systemie plików, w którym jest uruchomiony kod. Na przykład jest rozróżniana wielkość liter w systemie plików NTFS (domyślny system plików systemu Windows) i rozróżniana jest wielkość liter w systemach plików systemu Linux.

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

Dotyczy

Zobacz też