Directory.GetLastWriteTime(String) Método

Definição

Retorna a data e hora em que o arquivo ou diretório especificado foi usado para gravação pela última vez.

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

Parâmetros

path
String

O arquivo ou diretório para o qual obter informações de data e hora da modificação.

Retornos

DateTime

Uma estrutura que está definida para a data e hora em que o arquivo ou diretório especificado foi gravado pela última vez. Esse valor é expresso no horário local.

Exceções

O chamador não tem a permissão necessária.

.NET Framework e versões do .NET Core com mais de 2.1: path é uma cadeia de caracteres de comprimento zero, contém apenas espaço em branco ou contém um ou mais caracteres inválidos. Consulte caracteres inválidos com o método GetInvalidPathChars().

path é null.

O caminho especificado, o nome de arquivo, ou ambos excedem o tamanho máximo definido pelo sistema.

Exemplos

O exemplo a seguir demonstra como usar 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

Comentários

Observação

Esse método pode retornar um valor impreciso, pois usa funções nativas cujos valores podem não ser atualizados continuamente pelo sistema operacional.

Se o diretório descrito no path parâmetro não existir, este método retornará 12:00 meia-noite, 1º de janeiro de 1601 D.C. (C.E.) Tempo Universal Coordenado (UTC), ajustado para a hora local.

O path parâmetro tem permissão para especificar informações de caminho relativas ou absolutas. As informações do caminho relativo são interpretadas como relativas ao diretório de trabalho atual. Para obter o diretório de trabalho atual, consulte GetCurrentDirectory.

O path parâmetro não diferencia maiúsculas de minúsculas.

Para obter uma lista de tarefas comuns de E/S, consulte Tarefas comuns de E/S.

Aplica-se a

Confira também