0

I have problem with FileSystemWatcher class. I Have this code:

RunningPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Reporter\\";
XMLConfigWatcher = new FileSystemWatcher(RunningPath);
XMLConfigWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
XMLConfigWatcher.Filter = "database.xml";
XMLConfigWatcher.Changed += new FileSystemEventHandler(XMLConfigChanged);
XMLConfigWatcher.EnableRaisingEvents = true;

I want to observe one file - database.xml file. It it small file. I have win XP and it doesn't running on my computer, but my friend has Win 7 and it fires property. What's the reason and how to correct it?

And, of course, I attach Changed Event:

public void XMLConfigChanged(object sender, FileSystemEventArgs e)
{ 
    MessageBox.Show("Config changed from outside.");
    foreach (ProgramToGrid f in collection)
    {
        f.watcher.finalize();
    }

    App.Current.Dispatcher.Invoke((Action)delegate
    {
        collection.Clear();
    });

    XmlAccess xml = XmlAccess.getInstance();

    foreach (Program f in xml.getApps())
    {
        ProgramToGrid ptg = new ProgramToGrid(f.getFolder(), f.getMail(), f.getSubject());
        ptg.watcher = new BugWatcher(f.getFolder(), f.getMail(), f.getSubject());

        App.Current.Dispatcher.Invoke((Action)delegate
        {
            collection.Add(ptg);
        });
    }
}
hbsrud
  • 333
  • 2
  • 12
user3025978
  • 477
  • 2
  • 8
  • 27
  • Might be related to this answer here: http://stackoverflow.com/questions/8305444/windows-xp-application-data-folder – Baldrick Feb 12 '14 at 15:26
  • Hi! I don't think so, due to the Environment.SpecialFolder.ApplicationData is valid for me. I printed it, and it is correct path. – user3025978 Feb 12 '14 at 15:43
  • what do you mean you printed it? try if (System.IO.Directory.Exists(RunningPath)) – Lawrence Thurman Feb 12 '14 at 15:45
  • I write this: if (System.IO.Directory.Exists(RunningPath)) { MessageBox.Show(RunningPath); } And it show a message with correct path – user3025978 Feb 12 '14 at 15:49
  • I also confirm - FileSystemWatcher seems does not work at all! :-( So, I have to use BackgroundWorker and FileInfo classes to explicitly compare file parameters to see if there was any changes. I think there is wrong code sample in MS site for FileSystemWatcher. – dmitry_bond Jan 15 '21 at 13:10

0 Answers0