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

3

我知道FileSystemWatcher类在Windows 8上无法使用。为什么FileSystemWatcher可以检测到Windows 7上的属性更改,但无法在Windows 8上检测到?

不管怎样,我需要知道目录中的文件何时被更改。例如,我在计算机上安装了Dropbox,当我更新文件时,它会开始同步。Dropbox如何知道Windows 8中的文件已更改?

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


1
你确定 FileSystemWatcher 不起作用吗?根据 MSDN 的说法,它是被支持的。http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx - Greg
它可以工作,但当文件被修改时,它并不会触发事件。只有当文件被创建或删除时才会触发事件。或者我做错了什么。 - Tono Nam
FileSystemWatcher不会监视文件,它只监视目录。具体来说,它会在“dir”命令的结果发生变化时通知您。文件可能会更改,但不会影响“dir”的输出。 - Raymond Chen
如果问题仍然存在,请检查以下问题:https://dev59.com/ZGYr5IYBdhLWcg3wfKP_#23704476 - Stein Åsmul
2个回答

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

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中使用了这段代码,并且表现如预期。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接