3

I know that the class FileSystemWatcher does not work on windows 8. Why are FileSystemWatcher Attribute changes detected on Windows 7 but not Windows 8?

Anyways I need to know when a file is changed within a directory. For example I have dropbox installed on my computer and the moment I update a file it starts synchronizing. How does dropbox knows when a file has changed in windows 8?

I already tried this solution in c++ http://msdn.microsoft.com/en-us/library/aa365261 and I have the same problem as FileSystemWatcher. The problem seems to be from windows 8 instead of the class FileSystemWatcher. What work around solution can I take?

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 1
    Are you sure `FileSystemWatcher` doesn't work, according to MSDN it is supported? http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx – Greg May 16 '14 at 18:39
  • It works but it does not raise an event when a file is modified. It only raises the event when a file is created or deleted. Or maybe I am doing something wrong – Tono Nam May 16 '14 at 18:51
  • FileSystemWatcher does not watch files. It watches directories. In particular, it tells you when the result of a "dir" has changed. Files can change without affecting the output of "dir". – Raymond Chen May 16 '14 at 18:58
  • Check these issues too if the problem persists: http://stackoverflow.com/a/23704476/129130 – Stein Åsmul May 16 '14 at 21:17

2 Answers2

1

Here's some code I've used before to wait for a new dll to be compiled and then copy it to some target folder, and it seems to work okay.

static void StartWatching(string path)
{
    var watcher = new FileSystemWatcher();
    watcher.Path = path;
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName |
                           NotifyFilters.DirectoryName;
    watcher.Changed += watcher_Created;
    watcher.Created += watcher_Created;
    watcher.EnableRaisingEvents = true;

    var copier = new Thread(ConsumeOutOfTheFilesToCopyQueue);
    copier.Start();
}

    static void watcher_Created(object sender, FileSystemEventArgs e)
    {
        if (e.Name.Contains("whatever.dll"))
            if (!_filesToCopy.Contains(e.FullPath))
                lock (_syncRoot)
                    if (!_filesToCopy.Contains(e.FullPath))
                        _filesToCopy.Enqueue(e.FullPath);
    }
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
0

Yes that's correct. The FileSystemWatcher watches directories and it raises events relating to them. But the information in the event can be used for tracking files. Here is some code that I use to track the changes in an image file.

#region ----------------File System WATCHER ----------------------------
// this happens at construction time
FileSystemWatcher fileSystemWatcher = new System.IO.FileSystemWatcher();
fileSystemWatcher.Changed += new System.IO.FileSystemEventHandler(fileSystemWatcher_Changed);
fileSystemWatcher.Deleted += new System.IO.FileSystemEventHandler(fileSystemWatcher_Deleted);
fileSystemWatcher.Renamed += new System.IO.RenamedEventHandler(fileSystemWatcher_Renamed);

private void WatchFile(String fullFilePath)
{
    if (!File.Exists(fullFilePath))
        return;
    fileSystemWatcher.Path = Path.GetDirectoryName(fullFilePath);
    fileSystemWatcher.Filter = Path.GetFileName(fullFilePath);
    fileSystemWatcher.EnableRaisingEvents = true;
}
//    and those are the handlers
//
private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
    Bitmap bmp = null;
    FileInfo finfo = new FileInfo(m_currentFileName);
    if (!finfo.Exists)
        return;
    //Load and display the bitmap saved inside the text file/
    ------------ here ---------------
    // OR WHATEVER YOU NEED TO

}

private void fileSystemWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
    this.pictureBoxArea.BackgroundImage = null;
    fileSystemWatcher.EnableRaisingEvents = false;
    labelFileInfo.Text = "";
    MediaAvailablForUpload = false;
}

private void fileSystemWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
    pictureBoxArea.BackgroundImage = null;
    fileSystemWatcher.EnableRaisingEvents = false;
    labelFileInfo.Text = "";
}
#endregion

I have used this code in winxp, win7 and win8 and performed as expected.

Gogu CelMare
  • 699
  • 6
  • 15