C# 文件/文件夹监视器

发布于 2024-10-03 04:44:15 字数 291 浏览 1 评论 0 原文

我已经设法使用 FileSystemWatcher 查看文件和文件夹的更改。

我的问题是我无法区分文件和文件夹。文件和文件夹可能具有相同的路径名。

对于删除事件,我什至无法使用肮脏的解决方法来测试 File.Exists(path) 或 Directory.Exists(path),因为调用该方法时文件/文件夹已被删除。

也许这个对象有我需要的信息,但我没有找到它:

FileSystemEventArgs e

我只想知道更改的项目是文件还是文件夹。

I already managed to see file and folder changes with the FileSystemWatcher.

My problem is that I can't make a difference between files and folder. It's possible that a file and a folder have the same path names.

For the delete event I even can't use a dirty workarround with testing File.Exists(path) or Directory.Exists(path) because the file/folder is already deleted when the method is called.

Maybe this object has the info I need but I didn't found it:

FileSystemEventArgs e

I only want to know if the changed item was a file or a folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友欢 2024-10-10 04:44:15

假设它位于 NTFS 卷上,我认为您可以通过查看 更改日记。特别是 FSCTL_READ_USN_JOURNAL 控制代码并查找在 FileAttributesUSN_RECORD 查看它是否是 FILE_ATTRIBUTE_DIRECTORY

您可以在此处找到一个示例(使用 C++,但也可以转换为 C#,或者只是编写一个小型 C++ dll 以从您的应用程序调用):遍历变更日志记录的缓冲区

Assuming it's on a NTFS volume I think you could do what you need by looking at the Change Journals. Specifically the FSCTL_READ_USN_JOURNAL control code and looking at the FileAttributes of the USN_RECORD to see if it's a FILE_ATTRIBUTE_DIRECTORY.

You can find a sample here (in C++, but could possibly translate to C# or otherwise maybe just write a small C++ dll to call from your app): Walking a Buffer of Change Journal Records

许久 2024-10-10 04:44:15

您可以测试它是否有目录 属性

var attributes = File.GetAttributes(@"c:\somepath");
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
    // it's a directory
}
else
{
    // it's a file
}

当然,如果它已经被删除了,这将不起作用,你将无法判断类型。

You could test whether it has the Directory attribute:

var attributes = File.GetAttributes(@"c:\somepath");
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
    // it's a directory
}
else
{
    // it's a file
}

Of course if it has already been deleted this won't work and you won't be able to tell the type.

一萌ing 2024-10-10 04:44:15

我找到了一个干净且始终有效的解决方案:

观察者的标准设置是针对文件和文件夹的。这在我看来毫无意义,因为我无法找出更改后的对象的类型。

可以创建两个文件观察器。一种用于文件,一种用于文件夹。然后您只需更改默认设置,如下所示:

// for file
fileSysWatchFile.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
// for folder
fileSysWatchDir.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.LastWrite;

I've found a solution which is clean and always works:

The standard setting of a watcher is for files and folders. This makes no sense in my eyes since I can't find out which type the changed object had.

It's possible to create two filewatchers. One for files and one for folders. Then you just have to change the default settings as follows:

// for file
fileSysWatchFile.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
// for folder
fileSysWatchDir.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.LastWrite;
故人爱我别走 2024-10-10 04:44:15

除非您之前有一个路径->类型映射列表,您可以在其中查找已删除项目的最后类型,否则无法检索已删除项目的类型。

It is not possible to retrieve the type of the deleted item unless you had a list of path->type mappings before where you can look up the last type of the deleted item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文