Share via


Directory.GetLastWriteTime(String) Metoda

Definice

Vrátí datum a čas posledního zápisu zadaného souboru nebo adresáře.

public:
 static DateTime GetLastWriteTime(System::String ^ path);
public static DateTime GetLastWriteTime (string path);
static member GetLastWriteTime : string -> DateTime
Public Shared Function GetLastWriteTime (path As String) As DateTime

Parametry

path
String

Soubor nebo adresář, pro který chcete získat informace o datu a čase změny.

Návraty

Struktura, která je nastavena na datum a čas, do kterého byl zadaný soubor nebo adresář naposledy zapsán. Tato hodnota je vyjádřena v místním čase.

Výjimky

Volající nemá požadované oprávnění.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí metody se můžete dotazovat GetInvalidPathChars() na neplatné znaky.

path je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Příklady

Následující příklad ukazuje, jak použít GetLastWriteTime.

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      String^ path = "c:\\MyDir";
      if (  !Directory::Exists( path ) )
      {
         Directory::CreateDirectory( path );
      }
      else
      {
         
         // Take an action which will affect the write time.
         Directory::SetLastWriteTime( path, DateTime(1985,4,3) );
      }
      
      // Get the creation 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 which will affect the write time.
                Directory.SetLastWriteTime(path, new DateTime(1985,4,3));
            }

            // Get the creation 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 which will affect the write time.
        Directory.SetLastWriteTime(path, DateTime(1985, 4, 3))

    // Get the creation 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 which will affect the write time.
                Directory.SetLastWriteTime(path, New DateTime(1985, 4, 3))
            End If

            ' Get the creation 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

Poznámky

Poznámka

Tato metoda může vrátit nepřesnou hodnotu, protože používá nativní funkce, jejichž hodnoty nemusí být průběžně aktualizovány operačním systémem.

Pokud adresář popsaný v parametru path neexistuje, vrátí tato metoda 1. ledna 1601 n. 12:00 půlnoc (C.E.) Koordinovaný světový čas (UTC) upravený na místní čas.

Parametr path může zadat informace o relativní nebo absolutní cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Informace o získání aktuálního pracovního adresáře najdete v tématu GetCurrentDirectory.

Rozlišování velkých a malých písmen parametru path odpovídá rozlišení systému souborů, na kterém je kód spuštěn. Například nerozlišuje malá a malá písmena v systému souborů NTFS (výchozí systém souborů Windows) a v systémech souborů Linux se rozlišují malá a malá písmena.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Platí pro

Viz také