要查看英语原文,请勾选“英语”复选框。也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语
本文档已存档,并且将不进行维护。

RenamedEventArgs 类

Renamed 事件提供数据。

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

public class RenamedEventArgs : FileSystemEventArgs

RenamedEventArgs 类型公开以下成员。

  名称说明
公共方法RenamedEventArgs初始化 RenamedEventArgs 类的新实例。
页首

  名称说明
公共属性ChangeType获取所发生的目录事件的类型。 (继承自 FileSystemEventArgs。)
公共属性FullPath获取受影响的文件或目录的完全限定的路径。 (继承自 FileSystemEventArgs。)
公共属性Name获取受影响的文件或目录的名称。 (继承自 FileSystemEventArgs。)
公共属性OldFullPath获取受影响的文件或目录的前一个完全限定的路径。
公共属性OldName获取受影响的文件或目录的旧名称。
页首

  名称说明
公共方法Equals(Object)确定指定的 Object 是否等于当前的 Object (继承自 Object。)
受保护的方法Finalize允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法GetHashCode用作特定类型的哈希函数。 (继承自 Object。)
公共方法GetType获取当前实例的 Type (继承自 Object。)
受保护的方法MemberwiseClone创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法ToString返回表示当前对象的字符串。 (继承自 Object。)
页首

此类从 FileSystemEventArgs 继承,并通过这种方式扩展它:添加旧名称字段和旧完整路径字段,以指定受影响的文件或目录的前一个名称和完整路径。

下面的示例演示如何创建 FileSystemWatcher,以监视磁盘驱动器上文件的改动(创建、删除、重命名和更改)。 此示例还演示如何正确接收错误通知。


using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        //  Create a FileSystemWatcher to monitor all files on drive C.
        FileSystemWatcher fsw = new FileSystemWatcher("C:\\");

        //  Watch for changes in LastAccess and LastWrite times, and
        //  the renaming of files or directories. 
        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
            | NotifyFilters.FileName |NotifyFilters.DirectoryName;

        //  Register a handler that gets called when a 
        //  file is created, changed, or deleted.
        fsw.Changed += new FileSystemEventHandler(OnChanged);

        fsw.Created += new FileSystemEventHandler(OnChanged);

        fsw.Deleted += new FileSystemEventHandler(OnChanged);

        //  Register a handler that gets called when a file is renamed.
        fsw.Renamed += new RenamedEventHandler(OnRenamed);

        //  Register a handler that gets called if the 
        //  FileSystemWatcher needs to report an error.
        fsw.Error += new ErrorEventHandler(OnError);

        //  Begin watching.
        fsw.EnableRaisingEvents = true;

        Console.WriteLine("Press \'Enter\' to quit the sample.");
        Console.ReadLine();


    }

    //  This method is called when a file is created, changed, or deleted.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        //  Show that a file has been created, changed, or deleted.
        WatcherChangeTypes wct = e.ChangeType;
        Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
    }

    //  This method is called when a file is renamed.
    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        //  Show that a file has been renamed.
        WatcherChangeTypes wct = e.ChangeType;
        Console.WriteLine("File {0} {2} to {1}", e.OldFullPath, e.FullPath, wct.ToString());
    }

    //  This method is called when the FileSystemWatcher detects an error.
    private static void OnError(object source, ErrorEventArgs e)
    {
        //  Show that an error has been detected.
        Console.WriteLine("The FileSystemWatcher has detected an error");
        //  Give more information if the error is due to an internal buffer overflow.
        if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
        {
            //  This can happen if Windows is reporting many file system events quickly 
            //  and internal buffer of the  FileSystemWatcher is not large enough to handle this
            //  rate of events. The InternalBufferOverflowException error informs the application
            //  that some of the file system events are being lost.
            Console.WriteLine(("The file system watcher experienced an internal buffer overflow: " + e.GetException().Message));
        }
    }

}


.NET Framework

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

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2

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

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
显示: