如何在不不断扫描的情况下检测目录或文件何时发生更改

发布于 2024-09-09 00:51:39 字数 175 浏览 7 评论 0原文

除了读取所有文件并将它们与以前的快照进行比较之外,有没有办法在 Windows 中的 C# 中检测目录何时发生更改?如果需要的话,我不介意 PInvoke。

编辑 FileSystemWatcher 类很棒,但有一个问题是您必须启动后台任务/线程的回调通知中的所有工作,以避免阻塞,从而导致事件丢失。

Other than reading all the files and comparing them with a previous snapshot, is there a way to detect when a directory changes in C# with Windows? I don't mind PInvoke if that's what it takes.

EDIT The FileSystemWatcher class is great, but one gotcha is that you must kick off any work in the callback notification to a background task/thread to avoid blocking which will cause dropped events.

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

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

发布评论

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

评论(2

温柔嚣张 2024-09-16 00:51:39

使用 FileSystemWatcher 类 - 它可以满足您的需求。它不会告诉您文件中的哪些字节发生了更改,但它会告诉您哪些文件发生了更改。

来自文档:

使用FileSystemWatcher来监视
指定目录中的更改。你
可以监视文件的变化并
指定的子目录
目录。您可以创建一个组件
要观看本地计算机上的文件,
网络驱动器或远程计算机。

要监视所有文件中的更改,请设置
Filter 属性设置为空字符串
("") 或使用通配符 (".")。到
观看特定文件,设置过滤器
属性到文件名。为了
例如,观察
文件MyDoc.txt,设置过滤器
属性为“MyDoc.txt”。您还可以
观察某种类型的变化
文件。例如,要观看
文本文件发生变化,设置Filter
属性为“*.txt”。

您可以进行多种类型的更改
可以在目录或文件中监视。
例如,您可以观察变化
在属性中,LastWrite 日期和
时间,或文件大小或
目录。这是通过设置完成的
NotifyFilter 属性为其中之一
NotifyFilters 值。了解更多
有关您所做更改类型的信息
可以观看,参见NotifyFilters。

您可以监视重命名、删除、
或创建文件或目录。
例如,要监视重命名
文本文件,将 Filter 属性设置为
“*.txt”并调用 WaitForChanged
指定了 Renamed 的方法
它的参数。

Use the FileSystemWatcher class - it does what you want. It won't tell you which bytes in the file changed, but it will tell you which files have changes.

From the doc:

Use FileSystemWatcher to watch for
changes in a specified directory. You
can watch for changes in files and
subdirectories of the specified
directory. You can create a component
to watch files on a local computer, a
network drive, or a remote computer.

To watch for changes in all files, set
the Filter property to an empty string
("") or use wildcards ("."). To
watch a specific file, set the Filter
property to the file name. For
example, to watch for changes in the
file MyDoc.txt, set the Filter
property to "MyDoc.txt". You can also
watch for changes in a certain type of
file. For example, to watch for
changes in text files, set the Filter
property to "*.txt".

There are several types of changes you
can watch for in a directory or file.
For example, you can watch for changes
in Attributes, the LastWrite date and
time, or the Size of files or
directories. This is done by setting
the NotifyFilter property to one of
the NotifyFilters values. For more
information on the type of changes you
can watch, see NotifyFilters.

You can watch for renaming, deletion,
or creation of files or directories.
For example, to watch for renaming of
text files, set the Filter property to
"*.txt" and call the WaitForChanged
method with a Renamed specified for
its parameter.

萌逼全场 2024-09-16 00:51:39

我必须为一个程序执行此操作,该程序将监视目录并查看是否添加了任何新图像文件,然后它会自动调整它们的大小。当有人一次添加多个文件时,观察者不会捕获所有文件,因为它是单线程的,并且在删除另一个图像的同时忙于调整一个图像的大小。

我必须使其成为一个多线程应用程序,其中主线程仅监视目录并将文件添加到队列中,另一个线程将从队列中读取并调整这些图像的大小。

如果您要对这些文件执行任何操作,则可能需要小心这一点。

I've had to do this for a program that would watch a directory and see if any new image files were added, and it would then automatically resize them. When someone would add multiple files at one time, the watcher wouldn't catch all the files since it was single threaded and was busy resizing one image while another was being dropped.

I had to make this a multi-threaded app, where the main thread just watched the directory and added the files to a queue, and another thread would read from the queue and resize those images.

That's something you might want to be careful of if you're going to be doing anything with the files.

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