C# 了解windows 8上的文件何时更改

C# 了解windows 8上的文件何时更改,c#,windows-8,filesystemwatcher,C#,Windows 8,Filesystemwatcher,我知道FileSystemWatcher类在windows 8上不起作用 无论如何,我需要知道目录中的文件何时发生更改。例如,我的计算机上安装了dropbox,当我更新一个文件时,它就开始同步。dropbox如何知道windows 8中的文件何时发生了更改 我已经在C++中尝试过这个解决方案,我也有与FielSeStistWaWER相同的问题。问题似乎来自windows 8,而不是类FileSystemWatcher我可以采取什么变通解决方案?以下是我以前使用过的一些代码,用于等待编译新的dll

我知道FileSystemWatcher类在windows 8上不起作用

无论如何,我需要知道目录中的文件何时发生更改。例如,我的计算机上安装了dropbox,当我更新一个文件时,它就开始同步。dropbox如何知道windows 8中的文件何时发生了更改


我已经在C++中尝试过这个解决方案,我也有与FielSeStistWaWER相同的问题。问题似乎来自windows 8,而不是类FileSystemWatcher我可以采取什么变通解决方案?

以下是我以前使用过的一些代码,用于等待编译新的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);
    }

是的,没错。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中使用了此代码,并按预期执行。

您确定
FileSystemWatcher
不工作,根据MSDN,它是受支持的吗?它可以工作,但在修改文件时不会引发事件。它仅在创建或删除文件时引发事件。或者我做了一些错误的事情FileSystemWatcher不监视文件。它监视目录。特别是,它会告诉您“dir”的结果何时更改。文件可以更改而不影响“dir”的输出。如果问题仍然存在,请检查这些问题: