File.GetLastWriteTime Método

Definición

Sobrecargas

GetLastWriteTime(String)

Devuelve la fecha y hora a la que se escribió por última vez en el archivo o directorio especificado.

GetLastWriteTime(SafeFileHandle)

Devuelve la última fecha y hora de escritura del archivo o directorio especificados.

GetLastWriteTime(String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Devuelve la fecha y hora a la que se escribió por última vez en el archivo o directorio especificado.

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

Archivo o directorio para el que se va a obtener información de fecha y hora de escritura.

Devoluciones

Estructura DateTime que se establece en la fecha y hora a la que se escribió por última vez en el archivo o directorio especificado. Este valor se expresa en hora local.

Excepciones

El llamador no dispone del permiso requerido.

Versiones de .NET Framework y .NET Core anteriores a 2.1: path es una cadena de longitud cero, contiene solo espacios en blanco o contiene uno o varios caracteres no válidos. Puede consultar los caracteres no válidos con el método GetInvalidPathChars().

path es null.

La ruta de acceso especificada, el nombre de archivo o ambos superan la longitud máxima definida por el sistema.

path está en un formato no válido.

Ejemplos

En el ejemplo siguiente se muestra GetLastWriteTime.

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

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 dt2 = File.GetLastWriteTime path
printfn $"The last write time for this file was {dt2}."
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 some 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

Comentarios

Nota

Este método puede devolver un valor inexacto, ya que usa funciones nativas cuyos valores pueden no actualizarse continuamente por el sistema operativo. Cada sistema operativo administra la última hora de escritura según sus propias reglas. Para mejorar el rendimiento, es posible que un sistema operativo no establezca el último valor de hora de escritura en la hora exacta de la última operación de escritura, pero podría establecerlo en una aproximación cercana.

Si el archivo descrito en el path parámetro no existe, este método devuelve 12:00 medianoche, 1 de enero de 1601 A.D. (C.E.) Hora universal coordinada (UTC), ajustada a la hora local.

El path parámetro puede especificar información de ruta de acceso relativa o absoluta. La información de ruta de acceso relativa se interpreta como relativa al directorio de trabajo actual. Para obtener el directorio de trabajo actual, consulte GetCurrentDirectory.

Para obtener una lista de las tareas de E/S comunes, consulte Tareas de E/S comunes.

Consulte también

Se aplica a

GetLastWriteTime(SafeFileHandle)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Devuelve la última fecha y hora de escritura del archivo o directorio especificados.

public:
 static DateTime GetLastWriteTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle);
public static DateTime GetLastWriteTime (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);
static member GetLastWriteTime : Microsoft.Win32.SafeHandles.SafeFileHandle -> DateTime
Public Shared Function GetLastWriteTime (fileHandle As SafeFileHandle) As DateTime

Parámetros

fileHandle
SafeFileHandle

al SafeFileHandle archivo o directorio para el que se va a obtener la información de fecha y hora de última escritura.

Devoluciones

Estructura DateTime establecida en la última fecha y hora de escritura del archivo o directorio especificados. Este valor se expresa en hora local.

Excepciones

fileHandle es null.

El llamador no dispone del permiso requerido.

Se aplica a