windows 如何在没有持续扫描的情况下检测目录或文件何时发生更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3215619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:47:58  来源:igfitidea点击:

How do I detect when a directory or file changes without constant scanning

c#windowsfilefilesystemsdirectory

提问by jjxtra

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.

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

EDITThe 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.

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

回答by LBushkin

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

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

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.

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

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

您可以在目录或文件中观察多种类型的更改。例如,您可以观察属性、LastWrite 日期和时间或文件或目录大小的变化。这是通过将 NotifyFilter 属性设置为 NotifyFilters 值之一来完成的。有关您可以查看的更改类型的更多信息,请参阅 NotifyFilters。

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

回答by Makotosan

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.

如果您要对文件进行任何操作,则可能需要注意这一点。