【问题标题】:Know when a file changes on windows 8知道 Windows 8 上的文件何时更改
【发布时间】:2016-09-18 20:04:06
【问题描述】:

我知道 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 类。 我可以采取什么解决方法?

【问题讨论】:

  • 你确定FileSystemWatcher 不起作用,根据 MSDN 支持吗? msdn.microsoft.com/en-us/library/…
  • 它可以工作,但在修改文件时不会引发事件。它仅在创建或删除文件时引发事件。或者我做错了什么
  • FileSystemWatcher 不监视文件。它监视目录。特别是,它会告诉您“目录”的结果何时发生变化。文件可以更改而不影响“dir”的输出。
  • 如果问题仍然存在,请检查这些问题:stackoverflow.com/a/23704476/129130

标签: c# windows-8 filesystemwatcher


【解决方案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);
    }

【讨论】:

    【解决方案2】:

    是的,这是正确的。 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中都使用过这段代码,运行正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2021-12-24
      相关资源
      最近更新 更多