Tono Nam Tono Nam - 2 months ago 14
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++ 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?

Answer

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);
    }
Comments