Tono Nam
Tono Nam

Reputation: 36048

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?

Upvotes: 3

Views: 1048

Answers (2)

Gogu CelMare
Gogu CelMare

Reputation: 815

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.

Upvotes: 0

Matthew Groves
Matthew Groves

Reputation: 26141

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

Upvotes: 1

Related Questions