开发者

How to implement filesystem watcher?

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 h开发者_Python百科ow to proceed with this? Thanks.


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:

  1. the webserver thread loads the application
  2. your application starts the watcher and then finally output the web response.
  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); 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.

Lower level Win32 APIs information is here: Obtaining Directory Change Notifications

and here: FindFirstChangeNotification

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜