bslim

Hi all,

I had implement a filesystemwatcher where by it will watch a folder upon changed event.

But I am not sure why upon a file was generated in that folder, it will go into this event for 3 times.

I am expecting it to perform only 1 time.

Can anyone please help

Thanks



Re: .NET Base Class Library FileSystemWatcher

Manju Sandhu

Hi bslim,

Can you please provide me that code which you have written for filewatcher.





Re: .NET Base Class Library FileSystemWatcher

bslim

My code is as follow:

FileSystemWatcher watcher;

string WatchPath = "\\\\1.2.3.4\\DirA\\FolderA\\";

// Create a new FileSystemWatcher and set its properties.

watcher = new FileSystemWatcher();

watcher.Path = WatchPath;

/* Watch for changes in LastAccess and LastWrite times, and

the renaming of files or directories. */

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

| NotifyFilters.FileName | NotifyFilters.DirectoryName;

// Only watch text files.

watcher.Filter = "*.xml";

// Add event handlers.

watcher.Changed += new FileSystemEventHandler(OnChanged);

// Begin watching.

watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program.

Console.WriteLine("Press \'q\' to quit the sample.");

while(Console.Read()!='q');





Re: .NET Base Class Library FileSystemWatcher

Ji Cheng Wang - MSFT

Hi bslim,

The OnChanged event handler of FileSystemWatcher will be raised more than one time when there is a file or a folder changed. This is because the framework is passing through the native OS events, which may be any number of things, depending on the application. The work around way for this issue is to try to monitor the changed folder or file and then notify the framework. Try to check out the following sample codes for reference:

Code Block
static void Main(string[] args)
{
watcher = new FileSystemWatcher();
watcher.Path = mailbox;
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press Enter to quit\r\n");
Console.ReadLine();
}

public static void OnChanged(object source, FileSystemEventArgs e)
{
watcher.EnableRaisingEvents = false;
FileInfo objFileInfo = new FileInfo(e.FullPath);
if (!objFileInfo.Exists) return; // ignore the file open
ProcessAllFiles(e.FullPath);
}

Try to check out this article about "Workaround Double Callback of FileSystemWatcher Event Handler" for details - http://www.codeproject.com/useritems/FileWatcher.asp

Hope this helps,

Regards,

This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.
Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.