0

I am having a weird problem. I searched and tried to solve this but other solutions didn't worked for me. I am watching a folder. When there is any files created in that folder the created event calls. But weird this is that it getting called as the number of file present on that folder.

For example:

For the 1st file its called only once (which I want).

For the 2nd file its called twice.

For the 3rd file its called 3 times.

Code:

fw.Path = @"D:\xx\xx\" + fullName.GetStringPart(0, 3) + @"\" + fullName.GetStringPart(3, 2) + @"\" + fullName.Substring(5, 4) + @"\" + fullName;
fw.Filter = "*.*";
fw.NotifyFilter = NotifyFilters.CreationTime|
    NotifyFilters.FileName;
fw.Changed += new FileSystemEventHandler(OnFileChanged);
fw.Created += new FileSystemEventHandler(OnCreated);
fw.Deleted += new FileSystemEventHandler(OnDeleted);
fw.EnableRaisingEvents = true;

Event:

void OnCreated(object sender, FileSystemEventArgs e)
{
    try
    {
        fw.EnableRaisingEvents = false;
        while (!TestOpen(e.FullPath)) ;
        string str = e.FullPath;
        allImag.Add(str.Replace("Thumbs", "images"));
        allThumbImag.Add(e.FullPath);
        if (InvokeRequired)
            this.Invoke(new Action(() => this.addImage(e.FullPath)));
        else
            addImage(e.FullPath);
    }

    finally
    {
        fw.EnableRaisingEvents = true;
    }
}

Any guess?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • 2
    Can you post your code? – JMK May 21 '14 at 11:44
  • 1
    how do you create files? Maybe the program tries to do something to the existing files as well? – mcy May 21 '14 at 11:55
  • I am just moving the file. – Rashad May 21 '14 at 11:59
  • @Rashad Just a hunch, have you tried disabling antivirus? – JMK May 21 '14 at 12:00
  • Are you registering only once to those events? – Yuval Itzchakov May 21 '14 at 12:25
  • @YuvalItzchakov >> I think no. I am new how C#. How can I register these event once? – Rashad May 21 '14 at 12:31
  • MSDN: Some common occurrences, such as copying or moving a file or directory, do not correspond directly to an event, but these occurrences do cause events to be raised. When you copy a file or directory, the system raises a Created event in the directory to which the file was copied, if that directory is being watched. Hence it is recommended that you track your ways if the Create is called for the file that you have already processed or not. – codebased May 21 '14 at 12:47

1 Answers1

1

From the MSDN docs for FileSystemWatcher.Created Event:

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

There is no guarantee the event will occur only once. It might be caused by external processes monitoring your folders, such as an AntiVirus of some sort. I would look into that since the code looks fine.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321