如何检测时,没有恒定的扫描目录或文件的变化 [英] How do I detect when a directory or file changes without constant scanning

查看:137
本文介绍了如何检测时,没有恒定的扫描目录或文件的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了阅读完所有的文件,并将它们以previous快照相比,是有办法来检测时,目录的变化在C#中使用Windows?我不介意的PInvoke如果这是什么需要。

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.

推荐答案

使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx\">FileSystemWatcher类 - 它你想要做什么。该字节在文件发生变化,它不会告诉你,但它会告诉你哪些文件具有变化。

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.

从DOC:

使用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.

要监视所有文件中的变化,设定
  Filter属性为空字符串
  ()或使用通配符( 的)。至
  看一个具体的文件,设置过滤器
  属性的文件名。对于
  例如,为了观看在变化
  文件MyDoc.txt,设置过滤器
  属性设置为MyDoc.txt。你也可以
  观看用于在特定类型的变化
  文件。例如,以观察
  在文本文件中的变化,设置过滤器
  财产的* .txt。

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

有几种类型的变化的你
  可以看为一个目录或文件。
  例如,您可以观看更改
  在属性中,LastWrite日期
  时间,或文件的大小或
  目录。这是通过设置
  该NotifyFilter属性之一
  在NotifyFilters值。欲了解更多
  在变化的类型信息你
  可以看,看NotifyFilters。

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.

您可以观看重命名,删除,
  或创建文件或目录。
  例如,为了观看为重命名
  文本文件,设置过滤器属性
  的* .txt,并调用WaitForChanged
  方法用一个重命名指定
  它的参数。

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.

这篇关于如何检测时,没有恒定的扫描目录或文件的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆