Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fileSystemWatcher problem

Status
Not open for further replies.

Nejibg

Programmer
Apr 4, 2008
16
GB
Hi,
I worte a Csharp application that monitors a shared folder on our network using fileSystemWatcher component, the problem is that if the network connection is lost for few seconds fileSystemWatcher stops monitoring without raising any errors at all! And it doesn't start monitoring when connection is back.
(The same thing happens if you are monitoring files in a local folder and you delete it, fileSystemWatcher won't error, and won't restart monitoring if you put the folder back)
How can I force fileSystemWatcher to detect the problem and start monitoring as soon as the network connection is re-established?
Thanks in advance
 
Hi Nejibg,

I experienced the same issue and spent a lot of time googling and trying to find a solution to no avail. In the end I had to implement a horrible solution and poll the share.

I hope you have better luck in solving this - if you do, please post back the resolution!

Thanks,

MadJock

"Just beacuse you're paranoid, don't mean they're not after you
 
Hi,
Thanks for your reply.
How are you polling the share?
This code below would be ideal if the onError event would fire when the connection with the shared folder is lost.
I'll post here again if I find anymore info, I hope you'll do the same.
Thanks.

=======================
using System;
using System.IO;

namespace ConsoleApplication1
{
class Class1
{
private static FileSystemWatcher watcher =
new FileSystemWatcher();

[STAThread]
static void Main(string[] args)
{
WatchFile();
Console.ReadLine();
}

// set up the FileSystemWatcher object
private static void WatchFile()
{
// ***** Change this as required
watcher.Path = @"\\myServer\c$\test\";

// For this example, I only care about when new files are
// created
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.txt";

// Add event handlers: 1 for the event raised when a file
// is created, and 1 for when it detects an error.
watcher.Changed += new FileSystemEventHandler(NewFile);
watcher.Error += new ErrorEventHandler(WatcherError);

// Begin watching.
watcher.EnableRaisingEvents = true;
}

// Define the found event handler.
private static void NewFile(object source, FileSystemEventArgs e)
{
Console.WriteLine("A file has been found!");
File.Delete(e.FullPath);
}

// The error event handler
private static void WatcherError(object source, ErrorEventArgs e)
{
Exception watchException = e.GetException();
MessageBox.Show("A FileSystemWatcher error has occurred: "
+ watchException.Message);
// We need to create new version of the object because the
// old one is now corrupted
watcher = new FileSystemWatcher();
while (!watcher.EnableRaisingEvents)
{
try
{
// This will throw an error at the
// watcher.NotifyFilter line if it can't get the path.
WatchFile();
Console.WriteLine("I'm Back!!");
}
catch
{
// Sleep for a bit; otherwise, it takes a bit of
// processor time
System.Threading.Thread.Sleep(5000);
}
}
}
}
}


=======================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top