FileSystemInfo.LastWriteTime 属性

获取或设置上次写入当前文件或目录的时间。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Property LastWriteTime As DateTime
用法
Dim instance As FileSystemInfo
Dim value As DateTime

value = instance.LastWriteTime

instance.LastWriteTime = value
public DateTime LastWriteTime { get; set; }
public:
property DateTime LastWriteTime {
    DateTime get ();
    void set (DateTime value);
}
/** @property */
public DateTime get_LastWriteTime ()

/** @property */
public void set_LastWriteTime (DateTime value)
public function get LastWriteTime () : DateTime

public function set LastWriteTime (value : DateTime)

属性值

上次写入当前文件的时间。

异常

异常类型 条件

IOException

Refresh 不能初始化数据。

PlatformNotSupportedException

当前操作系统不是 Microsoft Windows NT 或更高版本。

备注

第一次调用时,FileSystemInfo 调用 Refresh 并返回 API 上的缓存信息以获取属性等。以后调用时,必须调用 Refresh 以获取信息的最新副本。

如果包含该文件的文件系统不支持此信息,则此属性值为 空引用(在 Visual Basic 中为 Nothing)。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

重命名或移动文件。

File.Move

FileInfo.MoveTo

获取文件属性。

File.GetAttributes

设置文件属性。

File.SetAttributes

Windows 95, Windows 98, Windows 98 Second Edition 平台说明: 这些操作系统不支持此属性,因此不支持此属性的 DirectoryInfo 实现。

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 此属性为只读。

示例

下面的代码示例演示通过 "touch" 操作更新 LastWriteTime 属性。在此例中,文件为 "touched",将 CreationTimeLastAccessTimeLastWriteTime 属性更新为当前日期和时间。

Imports System
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
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);
            }
        }
    }
}

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

FileSystemInfo 类
FileSystemInfo 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本