如何检测文件何时放入我的文件夹之一
在我的软件中,我将在我的文件夹之一中收到一个 XML 文件和一个 PDF 文件,因此我想启动一个事件来读取 XML,以便我可以管理这些文件。
我怎样才能检测到这个事件,你能给我推荐一本关于事件的书或一个页面,也许给我一个例子。
in my software I'm going to receive in one of my folders an XML file and a PDF file so i want to launch an event to read the XML so i can manage this files.
how can i detect this event, can u suggest me a book about events or a page, maybe gimme an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要使用 FileSystemWatcher 。
You want to use FileSystemWatcher .
如前所述,
FileSystemWatcher
是可行的方法。但请注意,它有一些微妙之处:创建的每个文件都会引发一次
Created
事件,但文件首次出现在文件夹中时就会引发该事件。如果您有一个不同的进程来复制该文件(可能是通过网络连接),如果您在处理 Created 事件时尝试访问该文件,则可能会遇到异常。另请注意,如果另一个进程正在复制您正在观看的文件夹中的文件,则很可能会多次引发
Changed
事件。使用NotifyFilter
减少引发的事件数量。as mentioned before,
FileSystemWatcher
is the way to go.note, however, that there are a few subtleties to it: the
Created
event is raised once per file created, but it is raised as soon as the file first appears in the folder. if you have a different process that copies that file, perhaps over a network connection, if you try to access the file while handling theCreated
event, you may get an exception.Also note that the
Changed
event is most likely going to be raised multiple times if another process is copying a file in the folder you're watching. Use theNotifyFilter
to reduce the number of events that are raised.您可以使用 FileSystemWatcher 类来监视该文件夹XML 文件。
You can use the FileSystemWatcher class to watch that folder for XML files.