`FindFirstChangeNotification` 函数的 `bWatchSubtree` 参数是什么意思?

What does it mean `bWatchSubtree` parameter of the `FindFirstChangeNotification` function?

我想用FindFirstChangeNotification监控一个文件夹,我不明白bWatchSubtree参数的意思。 “子树”是什么意思?是指被监控文件夹的子文件夹,还是被监控文件夹到盘符的父文件夹?

微软说:

If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory; if it is FALSE, it monitors only the specified directory.

假设我们有以下路径:D:\Software\Programming\Delphi\RADStudio

如果我监视“D:\Software\Programming”并且该参数是True,除了“Programming”之外我还会收到哪些文件夹的通知? (软件)或(Delphi 和 RADStudio)?

我做了一些测试,当我更改“Delphi 或 RADStudio”文件夹中的某些内容时,我没有收到任何通知。但是如果我删除“软件”文件夹,我会收到通知。如果我想更改它的名称,系统不会让我这样做,这非常令人沮丧。我希望在监视文件夹时能够对文件和文件夹进行任何操作。如果我更改其中一个父文件夹的名称,通知我并停止监视该文件夹会很好...

文件系统是一个数学tree以目录为根的子树因此表示由该目录及其所有后代组成的子树,即任何级别的所有子目录。

例如,

                          C:\
                           │
           ┌───────────────┴──────────────┐
        Letters                        Pictures
           │                              │
  ┌────────┴────────┐            ┌────────┴────────┐
Family             Work         Cats              Dogs
                                                   │
                                          ┌────────┴────────┐
                                        Small              Large

Pictures为根的子树由PicturesCatsDogsSmallLarge组成,而子树植根于 DogsDogsSmallLarge.

组成

我刚刚试过了,根据这个解释,FindFirstChangeNotification 函数及其 bWatchSubtree 参数确实按预期工作。

我使用了以下非常快速和肮脏的代码来测试它:

procedure TForm1.FormCreate(Sender: TObject);
var
  h: THandle;
begin
  h := FindFirstChangeNotification('C:\Pictures', True, FILE_NOTIFY_CHANGE_FILE_NAME);
  if h = INVALID_HANDLE_VALUE then
    RaiseLastOSError;
  TThread.CreateAnonymousThread(
    procedure
    begin
      while True do
        if WaitForSingleObject(h, INFINITE) = WAIT_OBJECT_0 then
        begin
          OutputDebugString('Yes!');
          FindNextChangeNotification(h);
        end;
    end
  ).Start
end;

如果我重命名任何图片,我会收到 Yes! 消息。不管是狗还是猫的照片,也不管狗是小还是大。使用 bWatchSubtree = False,我只会在 Pictures 文件夹 中的文件 被重命名时收到通知。未检测到重命名狗或猫图片。