Tono Nam Tono Nam - 1 year ago 107
C# Question

Know when a file changes on windows 8

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++ 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?

Answer Source

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 |
    watcher.Changed += watcher_Created;
    watcher.Created += watcher_Created;
    watcher.EnableRaisingEvents = true;

    var copier = new Thread(ConsumeOutOfTheFilesToCopyQueue);

    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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download