导出 (0) 打印
全部展开
此文章由人工翻译。 将光标移到文章的句子上,以查看原文。
译文
原文

ErrorEventArgs 类

Error 事件提供数据。

System.Object
  System.EventArgs
    System.IO.ErrorEventArgs

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

public class ErrorEventArgs : EventArgs

ErrorEventArgs 类型公开以下成员。

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

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

ErrorEventArgs 包含导致 Error 事件的 Exception 此类提供检索异常的 GetException 方法。

下面的示例创建 ErrorEventArgs 的新实例,并使用 Exception 对其进行初始化。 然后,该示例调用 GetException 来检索 Exception 并显示错误信息。 没有与此代码相关联的窗体。


public static void Main(string[] args) {
    // Creates an exception with an error message.
    Exception myException= new Exception("This is an exception test");

    // Creates an ErrorEventArgs with the exception.
    ErrorEventArgs myErrorEventArgs = new ErrorEventArgs(myException);

    // Extracts the exception from the ErrorEventArgs and display it.
    Exception myReturnedException = myErrorEventArgs.GetException();
    MessageBox.Show("The returned exception is: " + myReturnedException.Message);
 }
    


下面的示例演示如何创建 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) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

社区附加资源

添加
显示:
© 2014 Microsoft