Search code examples
c#filesystemwatcherunc

FileSystemWatcher() problem when monitoring mapped UNC


I have a problem with the FileSystemWatcher() Class. It works flawlessly when I'm monitoring a local file on my harddrive. When I change to a mapped UNC, it does not fire anymore. The UNC is mapped to a local drive (X:), with the NET USE command where the user and password are supplied, this is done in a batch file at startup. Anyone that knows why this doesn't work? I have checked the paths, all of them are correct, so the problem should be related to something else...

fw = new FileSystemWatcher();
        fw.Path = fileInfoPath;
        fw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        fw.Filter = fileInfoName;
        fw.Changed += new FileSystemEventHandler(FileOnChanged);
        fw.Created += new FileSystemEventHandler(FileOnChanged);

Help appreciated! :)


Solution

  • My solution to the problem was to leave the FileSystemWatcher() and create my own little watcher. It's very simple, but the only thing I wanted to watch was when the file was rewritten and then perform some action.

    This is basically what I do (removed try/catch and some invoking of other threads):

    System.Threading.Timer tFileWatcher;
    private string fileTime1 = "";
    private string fileTime2 = "";
    //
    private void Form1_Load(object sender, EventArgs e)
        {
            tFileWatcher = new System.Threading.Timer(ComputeBoundOp2, 0, 0, 500);
            fileTime1 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
            fileTime2 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
        }
    
    private void ComputeBoundOp2(Object state)
        {
            fileTime2 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
    
            if (fileTime1 != fileTime2)
            {
            //Do something
            }
        }