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?