为 FileSystemWatcher 添加更改事件的正确方法是什么?

Which is the right way to add the Change Event for a FileSystemWatcher?

添加已更改事件的正确方法是什么?

    public static void WatchFileForChange()
        {
            FileSystemWatcher fsw = new FileSystemWatcher();

            fsw.Path = Path.Join(System.Configuration.ConfigurationManager.AppSettings["AccessDBFolder"]);
            fsw.Changed += new FileSystemEventHandler(UpdateMDBChange);
            fsw.Filter = "*.mdb";
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
            fsw.EnableRaisingEvents = true;
            fsw.IncludeSubdirectories = true;
        }

        private static void UpdateMDBChange(object sender, FileSystemEventArgs e)
        {
            //
            DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);
            if (lastWriteTime.Ticks - lastRead.Ticks > 100000)
            {
                TestClient().Wait();
                lastRead = lastWriteTime;
            }
        }

fsw.Changed += new FileSystemEventHandler(UpdateMDBChange);还是fsw.Changed += UpdateMDBChange;

这两种方式似乎都可以。但我不知道是否会出现技术问题。或者如果某种方式不受欢迎。

它们在引擎盖下是相同的。

这是反汇编:

            ed.MyEvent += Ed_MyEvent;
00007FFDF3D05F22  mov         rcx,7FFDF3DD4F00h  
00007FFDF3D05F2C  call        CORINFO_HELP_NEWSFAST (07FFE5384AF00h)  
00007FFDF3D05F31  mov         qword ptr [rbp+28h],rax  
00007FFDF3D05F35  mov         r8,offset CLRStub[MethodDescPrestub]@7ffdf3cfc768 (07FFDF3CFC768h)  
00007FFDF3D05F3F  mov         rcx,qword ptr [rbp+28h]  
00007FFDF3D05F43  xor         edx,edx  
00007FFDF3D05F45  mov         r9,offset CLRStub[StubLinkStub]@7ffdf3bcd070 (07FFDF3BCD070h)  
00007FFDF3D05F4F  call        Method stub for: System.MulticastDelegate.CtorOpened(System.Object, IntPtr, IntPtr) (07FFDF3CF4568h)  
00007FFDF3D05F54  mov         rcx,qword ptr [rbp+38h]  
00007FFDF3D05F58  mov         rdx,qword ptr [rbp+28h]  
00007FFDF3D05F5C  cmp         dword ptr [rcx],ecx  
00007FFDF3D05F5E  call        CLRStub[MethodDescPrestub]@7ffdf3d059b8 (07FFDF3D059B8h)  
00007FFDF3D05F63  nop  

            ed.MyEvent += new dlgEvent(Ed_MyEvent);
00007FFDF3D05F64  mov         rcx,7FFDF3DD4F00h  
00007FFDF3D05F6E  call        CORINFO_HELP_NEWSFAST (07FFE5384AF00h)  
00007FFDF3D05F73  mov         qword ptr [rbp+20h],rax  
00007FFDF3D05F77  mov         r8,offset CLRStub[MethodDescPrestub]@7ffdf3cfc768 (07FFDF3CFC768h)  
00007FFDF3D05F81  mov         rcx,qword ptr [rbp+20h]  
00007FFDF3D05F85  xor         edx,edx  
00007FFDF3D05F87  mov         r9,offset CLRStub[StubLinkStub]@7ffdf3bcd070 (07FFDF3BCD070h)  
00007FFDF3D05F91  call        Method stub for: System.MulticastDelegate.CtorOpened(System.Object, IntPtr, IntPtr) (07FFDF3CF4568h)  
00007FFDF3D05F96  mov         rcx,qword ptr [rbp+38h]  
00007FFDF3D05F9A  mov         rdx,qword ptr [rbp+20h]  
00007FFDF3D05F9E  cmp         dword ptr [rcx],ecx  
00007FFDF3D05FA0  call        CLRStub[MethodDescPrestub]@7ffdf3d059b8 (07FFDF3D059B8h)  
00007FFDF3D05FA5  nop  
}

我不假装知道所有这些说明是什么,但你可以看到它们是一样的。

所以你的问题的答案是,“没关系”。

这叫做方法组转换。它允许您替换更详细的

fsw.Changed += new FileSystemEventHandler(UpdateMDBChange);

通过直接提供方法名称

fsw.Changed += UpdateMDBChange;

基本上,它是语法糖,是后来添加的功能,因此您可以减少输入。可以推断事件处理程序的类型,不需要显式声明。