如何判断文件系统事件(删除,复制,创建)何时发生? [英] How to tell when a file system event (delete, copy, create) occur happen?

查看:68
本文介绍了如何判断文件系统事件(删除,复制,创建)何时发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileSystem.DeleteFile,FileSystem.createFile和FileSystem.CopyFile。有人能告诉我怎么知道这个过程何时完成?我删除文件的速度比我的代码可以跟上的速度快。所以我想知道如何完成删除文件的过程。



---------------

以下if语句将返回true(有时候文件不存在)。由于它在文件不存在时返回true,我收到错误。有没有更好的方法来确定何时删除文件?



 如果 System.IO.File.Exists(FILE_NameCopy)=  True  然后 
My.Computer.FileSystem.DeleteFile(FILE_NameCopy)

结束 如果

解决方案

面向事件的方法看起来并不像某些代码告诉你发生了什么。技术要好得多:您使用事件处理程序处理事件。请参阅:

http://msdn.microsoft.com/en-us /library/ms973905.aspx [ ^ ]。



现在,如何处理您感兴趣的活动?这是使用类 System.IO.FileSystemWatcher 完成的:

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx [ ^ ]。



但是,请注意一些问题。请参阅这些CodeProject文章:

FileSystemWatcher - 纯混乱(第1部分,共2部分) [ ^ ],

FileSystemWatcher - Pure Chaos(第2部分,共2部分) [ ^ ]。



-SA

i am using FileSystem.DeleteFile, FileSystem.createFile, and FileSystem.CopyFile. Can someone tell me how to know when the process is complete? I am delete file faster than my code can keep up. So i want to know how can i tell if the process of deleting a file for example is done.

---------------
the below if statement will return true (sometime when the file does not exist). As a result of it returning true while the file does not exist i get an error. Is there a better way to determine when i delete the file ?

If System.IO.File.Exists(FILE_NameCopy) = True Then
         My.Computer.FileSystem.DeleteFile(FILE_NameCopy)
         
     End If

解决方案

The event-oriented approach does not look like some code is "telling" you what happened. The technology is much better: you use event handlers to handle events. Please see:
http://msdn.microsoft.com/en-us/library/ms973905.aspx[^].

Now, how to handle the events you are interested in? This is done using the class System.IO.FileSystemWatcher:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx[^].

However, be aware of some problems. Please see these CodeProject articles:
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^],
FileSystemWatcher - Pure Chaos (Part 2 of 2)[^].

—SA


这篇关于如何判断文件系统事件(删除,复制,创建)何时发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆