简体   繁体   English

如何实现文件系统观察器?

[英]How to implement filesystem watcher?

I am having a situation like: 我遇到的情况是:

Suppose there is one folder "D:\\Main" which can contain any word file. 假设有一个文件夹“ D:\\ Main”,其中可以包含任何Word文件。 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. 我对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. 我的意思是,如果我手动将文件添加到文件夹中,它也应该执行相同的操作(文件可以通过Web以及手动上传)。我目前正在使用ASP.NET和C#在Web应用程序中工作,并且我对Windows有所了解应用。 Can any one suggest me how to proceed with this? 有人可以建议我如何进行此操作吗? Thanks. 谢谢。

You will need to create either a windows app or a windows service app. 您将需要创建Windows应用程序或Windows服务应用程序。 The FileSystemWatch won't survive on a web application. FileSystemWatch无法在Web应用程序上生存。 The reason is because web application functions by following the steps: 原因是因为Web应用程序通过以下步骤起作用:

  1. the webserver thread loads the application Web服务器线程加载应用程序
  2. your application starts the watcher and then finally output the web response. 您的应用程序启动观察程序,然后最终输出Web响应。
  3. After some idle time, the thread terminates the application, and thus your watcher. 空闲一段时间后,线程将终止应用程序,从而终止您的观察者。

When you have your windows app, you can add the CanRaiseEvents = true and w.Created += new System.IO.FileSystemEventHandler(w_Created); 当您拥有Windows应用程序时,可以添加CanRaiseEvents = truew.Created += new System.IO.FileSystemEventHandler(w_Created); to your watcher. 给你的观察者。 Do your processing in 做您的处理

    void w_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        // here
    }

I just happened to be researching something very similar. 我刚巧正在研究非常相似的东西。

But you answered your own question. 但是你回答了自己的问题。 FileSystemWatcher is likely what you want. FileSystemWatcher可能是您想要的。

Lower level Win32 APIs information is here: Obtaining Directory Change Notifications 较低级别的Win32 API信息在这里: 获取目录更改通知

and here: FindFirstChangeNotification 此处: FindFirstChangeNotification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM