gpt4 book ai didi

c# - 如何在不持续扫描的情况下检测目录或文件何时更改

转载 作者:IT王子 更新时间:2023-10-29 04:47:23 24 4
gpt4 key购买 nike

除了读取所有文件并将它们与之前的快照进行比较之外,是否有一种方法可以检测 C# 和 Windows 中的目录何时更改?如果需要 PInvoke,我不介意。

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

最佳答案

使用 FileSystemWatcher类 - 它做你想做的。它不会告诉您文件中的哪些字节发生了更改,但会告诉您哪些文件发生了更改。

来自文档:

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.

关于c# - 如何在不持续扫描的情况下检测目录或文件何时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215619/

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