ANP
ANP

Reputation: 15607

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 how to proceed with this? Thanks.

Upvotes: 0

Views: 1287

Answers (2)

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

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
    }

Upvotes: 1

selbie
selbie

Reputation: 104524

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

Upvotes: 0

Related Questions