【问题标题】:.net FileSystemWatcher does not pick up moved folders.net FileSystemWatcher 不拾取移动的文件夹
【发布时间】:2016-03-30 01:25:32
【问题描述】:

我发现很多关于移动文件的讨论(我对此没有任何问题),但没有任何特定于移动的文件夹(因此这篇文章)。

我有一个FileSystemWatcher 实例化如下:

  var fileWatcher = new FileSystemWatcher("C:\\mypath");
  fileWatcher.IncludeSubdirectories = true;
  fileWatcher.NotifyFilter = NotifyFilters.LastWrite
                               | NotifyFilters.FileName
                               | NotifyFilters.CreationTime
                               | NotifyFilters.Size;
   fileWatcher.Changed += OnChanged;
   fileWatcher.Created += OnChanged;
   fileWatcher.Deleted += OnDeleted;
   fileWatcher.Renamed += OnRenamed;
   fileWatcher.Error += WatcherOnError;
   fileWatcher.EnableRaisingEvents = true;

无论我对文件做什么,我都会按预期得到事件,但是如果我将文件夹(即使其中有文件)拖动(移动)到监视文件夹,则没有事件完全被提出。

我在 Windows 10 上运行(不确定其他版本的 win 是否表现相同)。

有人知道如何获取文件夹移动的通知吗?

【问题讨论】:

    标签: c# .net windows filesystemwatcher


    【解决方案1】:

    通过在 NotifyFilter 中不包含 NotifyFilters.DirectoryName,您明确排除了目录更改。

    这是文档中的link,但它只是暗示正确 :-) 我通过使用您的代码确认它,然后使用额外的标志。

    【讨论】:

    • 谢谢。添加该标志后,我现在会收到有关文件夹移动的通知(在查看标志列表时完全错过了看到该标志)。
    【解决方案2】:

    奇怪,这对我有用:

    void Main()
    {
        FileSystemWatcher fsw = new FileSystemWatcher(@"c:\Temp\");
        fsw.IncludeSubdirectories = true;
        fsw.Changed += new FileSystemEventHandler(OnChanged);
        fsw.EnableRaisingEvents = true;
    
        while (true) { }
    }
    
    void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    }
    

    【讨论】:

    • 感谢您的回复。是的,上述方法有效,因为没有设置 NotifyFilters,所以它必须默认为所有这些,包括下面提到的 NotifyFilters.DirectoryName
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多