FileSystemInfo.LastWriteTime Properti

Definisi

Mendapatkan atau mengatur waktu ketika file atau direktori saat ini terakhir ditulis.

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

Nilai Properti

Waktu file saat ini terakhir ditulis.

Pengecualian

Refresh() tidak dapat menginisialisasi data.

Sistem operasi saat ini bukan Windows NT atau yang lebih baru.

Pemanggil mencoba mengatur waktu tulis yang tidak valid.

Contoh

Contoh kode berikut menunjukkan pembaruan LastWriteTime properti melalui operasi "sentuhan". Dalam contoh ini, file "disentuh", memperbarui CreationTimeproperti , LastAccessTime dan LastWriteTime ke tanggal dan waktu saat ini.

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

Keterangan

Catatan

Metode ini dapat mengembalikan nilai yang tidak akurat karena menggunakan fungsi asli yang nilainya mungkin tidak terus diperbarui oleh sistem operasi.

Nilai LastWriteTime properti telah di-cache sebelumnya jika instans FileSystemInfo objek saat ini dikembalikan dari salah satu metode berikut DirectoryInfo :

Untuk mendapatkan nilai terbaru, panggil Refresh metode .

Jika file atau direktori yang dijelaskan dalam FileSystemInfo objek tidak ada, atau jika sistem file yang berisi file atau direktori ini tidak mendukung informasi ini, properti ini mengembalikan 12:00 tengah malam, 1 Januari 1601 A.D. (C.E.) Waktu Universal Terkoordinasi (UTC), disesuaikan dengan waktu lokal.

Berlaku untuk

Lihat juga