gpt4 book ai didi

c# - .NET FileSystemWatcher 检测 NTFS 安全变化

转载 作者:行者123 更新时间:2023-12-04 02:08:19 25 4
gpt4 key购买 nike

.NET FileSystemWatcher 的 Changed 事件 MSDN documentation说:

The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.

但是,当我尝试使用此类来捕获目录或文件的 NTFS 安全更改时,Changed 事件永远不会触发。

有没有办法不用轮询就可以做到这一点?

最佳答案

FileSystemWatcher 会监视安全权限更改。
当您设置 FileSystemWatcher.NotifyFilter 时,您需要包含 NotifyFilters.Security 标志。我尝试了下面的代码,更改了 Temp 文件夹中文件的权限。 Changed 事件被触发。

public static void Main()
{
var fileSystemWatcher = new FileSystemWatcher("C:\\Temp", "*.*");
fileSystemWatcher.NotifyFilter = NotifyFilters.Security;
fileSystemWatcher.Changed += fileSystemWatcher_Changed;
fileSystemWatcher.EnableRaisingEvents = true;
Thread.Sleep(-1);
}

private static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
}

关于c# - .NET FileSystemWatcher 检测 NTFS 安全变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com