File.SetLastWriteTime(String, DateTime) Metodo

Definizione

Imposta la data e l'ora dell'ultima scrittura nel file specificato.

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)

Parametri

path
String

File per cui impostare le informazioni sulla data e l'ora.

lastWriteTime
DateTime

Oggetto DateTime contenente il valore da impostare per la data e l'ora dell'ultima scrittura di path. Questo valore è espresso nell'ora locale.

Eccezioni

.NET Framework versioni di .NET Core precedenti alla 2.1: è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene uno o più path caratteri non validi. È possibile cercare i caratteri non validi usando il metodo GetInvalidPathChars().

path è null.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.

Il percorso specificato non è stato trovato.

Il chiamante non dispone dell'autorizzazione richiesta.

Il formato di path non è valido.

lastWriteTime specifica un valore non compreso nell'intervallo di date o ore consentito per l'operazione.

Esempio

Nell'esempio seguente viene file system il file specificato, creando il file se necessario, quindi viene impostata e recuperata l'ora dell'ultima scrittura del 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

Commenti

Il path parametro può specificare informazioni relative o assolute sul percorso. Le informazioni sul percorso relativo vengono interpretate come relative alla directory di lavoro corrente. Per ottenere la directory di lavoro corrente, vedere GetCurrentDirectory .

Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Si applica a

Vedi anche