Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have been working on an old project which uses the Global.asax file to watch a specified directory.

The problem is that, whenever a new file arrives to watched directory the method fsw_Changed(object sender, FileSystemEventArgs e) fires twice.

The code is as follows:

Thank you very much,
C#
public class Global : System.Web.HttpApplication
{
    private FileSystemWatcher fsw;

    protected void Application_Start(object sender, EventArgs e)
    {
        string monitorPath = this.Context.Server.MapPath("~/dir1/dir2/" + DateTime.Now.Year.ToString());
        Application.Add("watcher", new FileSystemWatcher(monitorPath));
        fsw = (FileSystemWatcher)Application["watcher"];
        fsw.EnableRaisingEvents = true;
        fsw.IncludeSubdirectories = false;

        fsw.Changed += new FileSystemEventHandler(fsw_Changed);
    }

    private void fsw_Changed(object sender, FileSystemEventArgs e)
    {
        //.......
        //.......
    }
}
Posted
Updated 29-Sep-10 4:08am
v2

1 solution

Try adding

fsw.NotifyFilter = NotifyFilters.LastAccess;


to your initialization code before the call to

fsw.Changed += new FileSystemEventHandler(fsw_Changed);


FileSystemWatcher.Changed Event Reference
 
Share this answer
 
Comments
codddy 30-Sep-10 8:51am    
thank you for the reply.

I have tried to use it, and I also tried some different NotifyFilters but the problem still continued.

Finaly, I used the classical counter variable approach, and now it seems to work well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900