如何实现文件系统监视器?
我遇到这样的情况:
假设有一个文件夹“D:\Main”,其中可以包含任何word 文件。在将任何文件添加到文件夹时,我需要解析该文件,并根据一些关键字,我需要将文件传输到其他一些子文件夹以及数据库的一些条目。
我对 FileSystemWatcher 了解一些。如果有一个像“检查并移动”这样的按钮,那么我知道在按钮单击事件上我可以做一些事情,但如何自动执行它。我的意思是,如果我手动将文件添加到文件夹中,它也应该执行相同的操作(文件可以通过网络上传,也可以手动上传)。我目前正在使用 asp.net 和 c# 进行网络应用程序工作,我对 Windows 有一点了解应用。任何人都可以建议我如何继续进行此操作吗?谢谢。
I am having a situation like:
Suppose there is one folder "D:\Main" which can contain any word file. On adding any file/files to the folder I need to parse the file and based on some keywords I need to transfer the file to some other sub folders as well as some entry to the database also.
I know something about FileSystemWatcher. If there would have been a button like "Check and Move" then I know that on the button click event I can do something but how to do it automatically. I mean if I add file to the folder manually also it should do the same(files may be uploaded via web as well as manually).I am currently working in web application with asp.net and c# and I have a little knowledge about windows application. Can any one suggest me how to proceed with this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要创建 Windows 应用程序或 Windows 服务应用程序。
FileSystemWatch
无法在 Web 应用程序上生存。原因是因为 Web 应用程序按照以下步骤运行:当您拥有 Windows 应用程序时,您可以将
CanRaiseEvents = true
和w.Created += new System.IO.FileSystemEventHandler(w_Created);
添加到您的观察程序。进行处理You will need to create either a windows app or a windows service app. The
FileSystemWatch
won't survive on a web application. The reason is because web application functions by following the steps:When you have your windows app, you can add the
CanRaiseEvents = true
andw.Created += new System.IO.FileSystemEventHandler(w_Created);
to your watcher. Do your processing in我只是碰巧正在研究一些非常相似的东西。
但你回答了你自己的问题。 FileSystemWatcher 可能就是您想要的。
较低级别的 Win32 API 信息如下:
获取目录更改通知
和此处:
FindFirstChangeNotification
I just happened to be researching something very similar.
But you answered your own question. FileSystemWatcher is likely what you want.
Lower level Win32 APIs information is here:
Obtaining Directory Change Notifications
and here:
FindFirstChangeNotification