知道Windows 8上的文件何时发生变化

时间:2014-05-16 18:33:42

标签: c# windows-8 filesystemwatcher

我知道类FileSystemWatcher在Windows 8上不起作用。Why are FileSystemWatcher Attribute changes detected on Windows 7 but not Windows 8?

无论如何,我需要知道何时在目录中更改文件。例如,我的计算机上安装了Dropbox,当我更新文件时,它开始同步。 dropbox如何知道Windows 8中的文件何时发生了变化?

我已经在c ++ http://msdn.microsoft.com/en-us/library/aa365261中尝试了这个解决方案,我遇到了与FileSystemWatcher相同的问题。问题似乎来自Windows 8而不是类FileSystemWatcher。 我可以采取哪些解决方案?

2 个答案:

答案 0 :(得分:1)

这是我之前用过的一些代码,等待编译一个新的dll然后将它复制到某个目标文件夹,它似乎工作正常。

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

答案 1 :(得分:0)

是的,这是对的。 FileSystemWatcher监视目录,并引发与它们相关的事件。但事件中的信息可用于跟踪文件。这是我用来跟踪图像文件中的更改的一些代码。

#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

我在winxp,win7和win8中使用了这段代码并按预期执行。