【问题标题】:FileSystemWatcher - minimum permissions needed on target directories?FileSystemWatcher - 目标目录所需的最低权限?
【发布时间】:2011-04-27 19:17:08
【问题描述】:

使用 .NET FileSystemWatcher http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx 监视一个充满文件的目录:已更改;创建;已删除;重命名事件。

运行 FileSystemWatcher 的帐户对其正在监视的目录所需的最低权限是多少?

它似乎是 READ,但我无法在任何地方找到它的文档。

谢谢

【问题讨论】:

    标签: c# permissions file-permissions filesystemwatcher


    【解决方案1】:

    如果 FileSystemWatcher 基于 ReadDirectoryChangesW 它需要:

    1. 要监控的目录上的FILE_LIST_DIRECTORY
    2. 特权SeBackupPrivilege 允许持有者在绕过访问检查的同时读取任何内容。对此的指示是 CreateFile 的标志 FILE_FLAG_BACKUP_SEMANTICS。

    这在上面链接的 ReadDirectoryChangesW 的描述中有记录。

    【讨论】:

      【解决方案2】:

      底层 API 是 ReadDirectoryChangesW。 MSDN 库文章中唯一提到的是目录句柄需要 FILE_LIST_DIRECTORY 访问权限,并且需要使用 FILE_FLAG_BACKUP_SEMANTICS 选项打开目录。

      .NET 框架代码通常很有帮助。私有 FileSystemWatcher.StartRaisingEvents() 方法使用此代码打开目录句柄:

      directoryHandle = NativeMethods.CreateFile(
          directory,                                 // Directory name
          UnsafeNativeMethods.FILE_LIST_DIRECTORY,   // access (read-write) mode
          UnsafeNativeMethods.FILE_SHARE_READ |
          UnsafeNativeMethods.FILE_SHARE_DELETE |
          UnsafeNativeMethods.FILE_SHARE_WRITE,      // share mode
          null,                                      // security descriptor
          UnsafeNativeMethods.OPEN_EXISTING,         // how to create
          UnsafeNativeMethods.FILE_FLAG_BACKUP_SEMANTICS |
          UnsafeNativeMethods.FILE_FLAG_OVERLAPPED,  // file attributes
          new SafeFileHandle(IntPtr.Zero, false));   // file with attributes to copy
      

      仅将 FILE_FLAG_OVERLAPPED 用于异步通知。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2015-05-19
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      相关资源
      最近更新 更多