FileSystemInfo.LastWriteTime Proprietà

Definizione

Recupera o imposta l'ora dell'ultima modifica del file o della directory corrente.

public:
 property DateTime LastWriteTime { DateTime get(); void set(DateTime value); };
public DateTime LastWriteTime { get; set; }
member this.LastWriteTime : DateTime with get, set
Public Property LastWriteTime As DateTime

Valore della proprietà

Ora dell'ultima modifica del file o della directory corrente.

Eccezioni

Refresh() non è in grado di inizializzare i dati.

Il sistema operativo corrente non è Windows NT o versioni successive.

Il chiamante tenta di impostare un'ora di scrittura non valida.

Esempio

Nell'esempio di codice seguente viene illustrato l'aggiornamento della LastWriteTime proprietà tramite un'operazione di tocco. In questo esempio il file viene "toccato", aggiornando le CreationTimeproprietà e LastAccessTimeLastWriteTime alla data e all'ora correnti.

using System;
using System.IO;

namespace touch
{
    class Touch
    {
        static void Main(string[] args)
        {
            // Make sure a filename was provided.
            if (args.Length > 0)
            {
                // Verify that the provided filename exists.
                if (File.Exists(args[0]))
                {
                    FileInfo fi = new FileInfo(args[0]);
                    touchFile(fi);
                }
                else
                {
                    Console.WriteLine(
                        "Could not find the file: {0}.", args[0]);
                }
            }
            else
            {
                Console.WriteLine("No file was specified.");
            }
        }

        static void touchFile(FileSystemInfo fsi)
        {
            Console.WriteLine("Touching: {0}", fsi.FullName);

            // Update the CreationTime, LastWriteTime and LastAccessTime.
            try
            {
                fsi.CreationTime = fsi.LastWriteTime = fsi.LastAccessTime =
                    DateTime.Now;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
    }
}
Imports System.IO

Public Class Touch
    Public Shared Sub Main(ByVal args() As String)

        ' Make sure an argument (filename) was provided.
        If args.Length > 0 Then

            ' Verify that the provided filename exists.
            If File.Exists(args(0)) Then
                Dim fi As FileInfo = New FileInfo(args(0))
                touchFile(fi)
            Else
                Console.WriteLine("Could not find the file {0}", args(0))
            End If
        Else
            Console.WriteLine("No file specified.")
        End If
    End Sub

    Public Shared Sub touchFile(ByVal fsi As FileSystemInfo)
        Console.WriteLine("Touching: {0}", fsi.FullName)

        ' Update the CreationTime, LastWriteTime and LastAccessTime.
        Try
            fsi.CreationTime = DateTime.Now
            fsi.LastAccessTime = DateTime.Now
            fsi.LastWriteTime = DateTime.Now
        Catch e As Exception
            Console.WriteLine("Error: {0}", e.Message)
        End Try

    End Sub

End Class

Commenti

Nota

Questo metodo può restituire un valore non accurato perché usa funzioni native i cui valori potrebbero non essere aggiornati continuamente dal sistema operativo.

Il valore della LastWriteTime proprietà viene pre-memorizzato nella cache se l'istanza corrente dell'oggetto FileSystemInfo è stata restituita da uno dei metodi seguenti DirectoryInfo :

Per ottenere il valore più recente, chiamare il Refresh metodo .

Se il file o la directory descritta nell'oggetto FileSystemInfo non esiste o se il file system che contiene questo file o directory non supporta queste informazioni, questa proprietà restituisce 12:00 mezzanotte, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adattato all'ora locale.

Si applica a

Vedi anche