It needs at least two things: 1) an event that is raised when the directory being watched disappears (by being deleted, or by network outages, when watching a network resource), and 2) a method to restart the FSW when the outage is recovers.
Otherwise, it's a very handy tool.
Discussion on:
View:
Show:
As far as directory being removed, you can put another watcher on the parent drive and watch for the deletion of the subdirectory. Then, only if your drive is removed do you have a problem.
As far as recovering from a network outage, you might be able to deal with that programmatically, maybe a periodic read to the network resource, flag when the outage is detected, then when the resource returns, dispose of and recreate the FileSystemWatcher. I haven't tried it but it seems possible.
As far as recovering from a network outage, you might be able to deal with that programmatically, maybe a periodic read to the network resource, flag when the outage is detected, then when the resource returns, dispose of and recreate the FileSystemWatcher. I haven't tried it but it seems possible.
anyway to monitor NAS storage running non MS OS?
Good one but with exceptions like outages as mentioned abv
The Eg. is a good one but with exceptions like outages mentioned abv which i totally agree.
Thanks a lot Zach. Your article is very comprehensive and was a great help to me. Cheers
How much event it can register at a time. n If it is configurable
hello have problem in filesystemwatcher in windows 2000 server with service pack 4.
filesystemwatcher no monitoring files not extension for example AB1_DiVivienda_*.* asigned filter no response.
Please helpme this problem
filesystemwatcher no monitoring files not extension for example AB1_DiVivienda_*.* asigned filter no response.
Please helpme this problem
I am using Created event of FileSystemWatcher. According to my business logic i have to move the file when it arrives. But when i do so i get an exception
The Process cannot access the file it is begin used by anther process". how i can prevent this situation.
The Process cannot access the file it is begin used by anther process". how i can prevent this situation.
This is because the file which has triggered the created event is likley still being copied to the target destination. Your code is trying to touch the file too quickly. I recommend putting a delay before accessing the file. I have a function which waits until the file becomes available, with a timeout of course:
private Boolean FileTransferComplete(string fullPath)
{
while (true)
{
try
{
using (StreamReader stream = new StreamReader(fullPath))
{
return true;
}
}
catch (Exception ex)
{
// If this file no longer exists then ignore it
if (ex.Message.Contains("Could not find file"))
{
return false;
}
Console.WriteLine("File is busy... Waiting for file to become available.");
Thread.Sleep(2000);
}
}
}
private Boolean FileTransferComplete(string fullPath)
{
while (true)
{
try
{
using (StreamReader stream = new StreamReader(fullPath))
{
return true;
}
}
catch (Exception ex)
{
// If this file no longer exists then ignore it
if (ex.Message.Contains("Could not find file"))
{
return false;
}
Console.WriteLine("File is busy... Waiting for file to become available.");
Thread.Sleep(2000);
}
}
}
- Keyboard Shortcuts:
- Prev
- Next
- Toggle









































