File.SetLastWriteTime(String, DateTime) File.SetLastWriteTime(String, DateTime) File.SetLastWriteTime(String, DateTime) File.SetLastWriteTime(String, DateTime) Method

Definición

Establece la fecha y la hora en que escribió por última vez en el archivo especificado.Sets the date and time that the specified file was last written to.

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)

Parámetros

path
String String String String

Archivo para el que se va a establecer información de fecha y hora.The file for which to set the date and time information.

lastWriteTime
DateTime DateTime DateTime DateTime

DateTime que contiene el valor que se va a establecer para la fecha y hora de última escritura de path.A DateTime containing the value to set for the last write date and time of path. Este valor se expresa en hora local.This value is expressed in local time.

Excepciones

path es una cadena de longitud cero que solo contiene espacios en blanco o uno o varios caracteres no válidos definidos por InvalidPathChars.path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

La ruta de acceso especificada, el nombre de archivo o ambos superan la longitud máxima definida por el sistema.The specified path, file name, or both exceed the system-defined maximum length.

No se encontró la ruta de acceso especificada.The specified path was not found.

El llamador no dispone del permiso requerido.The caller does not have the required permission.

path está en un formato no válido.path is in an invalid format.

lastWriteTime especifica un valor fuera del intervalo de fechas u horas permitidas para esta operación.lastWriteTime specifies a value outside the range of dates or times permitted for this operation.

Ejemplos

En el siguiente ejemplo se comprueba el sistema de archivos para el archivo especificado, se crea el archivo si es necesario y, a continuación, se establece y se obtiene la hora de la última escritura del archivo.The following example checks the file system for the specified file, creating the file if necessary, and then sets and gets the last write time of the file.

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

Comentarios

El path parámetro puede especificar información de ruta de acceso relativa o absoluta.The path parameter is permitted to specify relative or absolute path information. La información de la ruta de acceso relativa se interpreta en relación con el directorio de trabajo actual.Relative path information is interpreted as relative to the current working directory. Para obtener el directorio de trabajo actual, GetCurrentDirectoryvea.To obtain the current working directory, see GetCurrentDirectory.

Para obtener una lista de tareas de e/s comunes, consulte tareas comunes de e/s.For a list of common I/O tasks, see Common I/O Tasks.

Seguridad

FileIOPermission
para escribir en el archivo especificado.for writing to the specified file. Enumeración asociada:WriteAssociated enumeration: Write

Se aplica a

Consulte también: