ReadDirectoryChangesW的用法

Windows监控文件夹中文件变化 一、Windows提供了几种方式对文件和目录进行监控,包括:FindFirstChangeNotification、ReadDirectoryChangesW、变更日志(Change Journal)等。 (1)FindFirstChangeNotification函数,可以监控到目标目录及其子目录中所有文件的变化,但不能监控到具体是哪一个文件发生改变。 (2)ReadDirectoryChang... 阅读详情

Retrieves information that describes the changes within the specified directory. The function does not report changes to the specified directory itself.

To track changes on a volume, see change journals.

Syntax

BOOL WINAPI ReadDirectoryChangesW(
  __in         HANDLE hDirectory,
  __out        LPVOID lpBuffer,
  __in         DWORD nBufferLength,
  __in         BOOL bWatchSubtree,
  __in         DWORD dwNotifyFilter,
  __out_opt    LPDWORD lpBytesReturned,
  __inout_opt  LPOVERLAPPED lpOverlapped,
  __in_opt     LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);

hDirectory [in]

A handle to the directory to be monitored. This directory must be opened with the FILE_LIST_DIRECTORY access right.

Windows 2000:  Only one handle is serviced at a time. Additional handle requests are not serviced until the previous service request is completed.
lpBuffer [out]

A pointer to the DWORD-aligned formatted buffer in which the read results are to be returned. The structure of this buffer is defined by the FILE_NOTIFY_INFORMATION structure. This buffer is filled either synchronously or asynchronously, depending on how the directory is opened and what value is given to the lpOverlapped parameter. For more information, see the Remarks section.

nBufferLength [in]

The size of the buffer that is pointed to by the lpBuffer parameter, in bytes.

bWatchSubtree [in]

If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory. If this parameter is FALSE, the function monitors only the directory specified by the hDirectory parameter.

dwNotifyFilter [in]

The filter criteria that the function checks to determine if the wait operation has completed. This parameter can be one or more of the following values.

ValueMeaning
FILE_NOTIFY_CHANGE_FILE_NAME 0x00000001

Any file name change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a file.

FILE_NOTIFY_CHANGE_DIR_NAME 0x00000002

Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory.

FILE_NOTIFY_CHANGE_ATTRIBUTES 0x00000004

Any attribute change in the watched directory or subtree causes a change notification wait operation to return.

FILE_NOTIFY_CHANGE_SIZE 0x00000008

Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.

FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010

Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.

FILE_NOTIFY_CHANGE_LAST_ACCESS 0x00000020

Any change to the last access time of files in the watched directory or subtree causes a change notification wait operation to return.

FILE_NOTIFY_CHANGE_CREATION 0x00000040

Any change to the creation time of files in the watched directory or subtree causes a change notification wait operation to return.

FILE_NOTIFY_CHANGE_SECURITY 0x00000100

Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return.

 

lpBytesReturned [out, optional]

For synchronous calls, this parameter receives the number of bytes transferred into the lpBuffer parameter. For asynchronous calls, this parameter is undefined. You must use an asynchronous notification technique to retrieve the number of bytes transferred.

lpOverlapped [in, out, optional]

A pointer to an OVERLAPPED structure that supplies data to be used during asynchronous operation. Otherwise, this value is NULL. The Offset and OffsetHigh members of this structure are not used.

lpCompletionRoutine [in, optional]

A pointer to a completion routine to be called when the operation has been completed or canceled and the calling thread is in an alertable wait state. For more information about this completion routine, see FileIOCompletionRoutine.

Return Value

If the function succeeds, the return value is nonzero. For synchronous calls, this means that the operation succeeded. For asynchronous calls, this indicates that the operation was successfully queued.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If the network redirector or the target file system does not support this operation, the function fails with ERROR_INVALID_FUNCTION.

Remarks

To obtain a handle to a directory, use the CreateFile function with the FILE_FLAG_BACKUP_SEMANTICS flag.

A call to ReadDirectoryChangesW can be completed synchronously or asynchronously. To specify asynchronous completion, open the directory with CreateFile as shown above, but additionally specify the FILE_FLAG_OVERLAPPED attribute in the dwFlagsAndAttributes parameter. Then specify an OVERLAPPED structure when you call ReadDirectoryChangesW.

When you first call ReadDirectoryChangesW, the system allocates a buffer to store change information. This buffer is associated with the directory handle until it is closed and its size does not change during its lifetime. Directory changes that occur between calls to this function are added to the buffer and then returned with the next call. If the buffer overflows, the entire contents of the buffer are discarded and the lpBytesReturned parameter contains zero.

Upon successful synchronous completion, the lpBuffer parameter is a formatted buffer and the number of bytes written to the buffer is available in lpBytesReturned. If the number of bytes transferred is zero, the buffer was either too large for the system to allocate or too small to provide detailed information on all the changes that occurred in the directory or subtree. In this case, you should compute the changes by enumerating the directory or subtree.

For asynchronous completion, you can receive notification in one of three ways:

  • Using the GetOverlappedResult function. To receive notification through GetOverlappedResult, do not specify a completion routine in the lpCompletionRoutine parameter. Be sure to set the hEvent member of the OVERLAPPED structure to a unique event.
  • Using the GetQueuedCompletionStatus function. To receive notification through GetQueuedCompletionStatus, do not specify a completion routine in lpCompletionRoutine. Associate the directory handle hDirectory with a completion port by calling the CreateIoCompletionPort function.
  • Using a completion routine. To receive notification through a completion routine, do not associate the directory with a completion port. Specify a completion routine in lpCompletionRoutine. This routine is called whenever the operation has been completed or canceled while the thread is in an alertable wait state. The hEvent member of the OVERLAPPED structure is not used by the system, so you can use it yourself.

For more information, see Synchronous and Asynchronous I/O.

ReadDirectoryChangesW fails with ERROR_INVALID_PARAMETER when the buffer length is greater than 64 KB and the application is monitoring a directory over the network. This is due to a packet size limitation with the underlying file sharing protocols.

ReadDirectoryChangesW fails with ERROR_NOACCESS when the buffer is not aligned on a DWORD boundary.

Windows 2000:  Clients that attempt multiple simultaneous long-term requests against a server, for example change notifications, should be running Service Pack 2 or higher. See Knowledge Base article Q271148 for more details.

If you opened the file using the short name, you can receive change notifications for the short name.

Transacted Operations

If there is a transaction bound to the directory handle, then the notifications follow the appropriate transaction isolation rules. 

 

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

 

 

 

上面是MSDN的原文,摘要下:

 

1.该函数用来监控文件夹变化,基本步骤是:

    a.使用CreateFile打开目录,打开目录的时候一定要包含FILE_LIST_DIRECTORY参数,貌似是要取得一个什么权限;

    b.然后使用该函数监听,收到系统的消息后,从lpbuffer里面取得Action和Filename;

 

2.该函数可以同步调用或者异步调用,是同步还是异步取决于CreateFile时给的参数,和该函数的第7个参数LPOVERLAPPED lpOverlapped:

    a.同步调用,则CreateFile时使用FILE_LIST_DIRECTORY,第7个参数设置为nil;

    b.异步调用,则CreateFile时还要指定FILE_FLAG_OVERLAPPED,第7个参数还要给个值;

 

3.第三个参数和第四个参数设置缓冲区,最大64KB,如果监视目录下一次操作太多的文件,超过缓冲区(缓冲区溢出),就会丢失消息什么的;

ReadDirectoryChangesW 写成的 文件监控类 名称:CDirectoryWatch IDE:VS2013 功能: 1. 可开启多个文件监控。 2. 提供监控通知处理接口,使每个监控有单独的 处理函数 其他: 1. 使用异步ReadDirectoryChangesW 2. 使用消息处理 3. 还凑活的 调试反馈 阅读详情

相关推荐

ReadDirectoryChangesW监控文件夹

指定的一个目录进行监控,当该目录中有文件发生改变,并通知处理。 Windows提供了对文件和目录监控的系统服务,并且为应用程序提供了两个API函数,它们分别是:FindFirstChangeNotification和ReadDirectoryChangesW。其中FindFirstChangeNotification函数只能监控到某一目录下有文件发生改变,而不能监控到具体是哪一文件发生改变。Rea

niushitang的专栏 2275

深入了解ReadDirectoryChangesW并应用其监控文件目录

ReadDirectoryChangesW 是Windows提供一个函数,它属于Windows API的一部分,主要用于监视文件系统中目录的修改、新增、删除等变化,并通过回调函数向应用程序提供通知。

【Rainbow Network Technology co., LTD】 3054

ReadDirectoryChangesW---异步方式(IO完成端口)监控目录中的文件

同步方式处理的话如果多个文件有可能有处理不到的文件。采用异步方式可以解决这个问题。 基于IO完成端口实现的封装类class P2PFileShare: #if !defined(AFX_P2PFILESHARE_H__5CFE59EF_7A09_4715_885F_FED4E2992470__INCLUDED_) #define AFX_P2PFILESHARE_H__5CFE59EF...

小米的修行之路 4574

ReadDirectoryChangesW

ReadDirectoryChangesW 函数 对指定的目录进行监控,返回详细的文件变化信息。

savagelin的专栏 1376

手把手教你用Python实现双三次插值(附OpenCV对比测试)

本文深入解析了双三次插值算法的数学原理,并提供了从零开始的Python实现教程。通过与OpenCV官方实现的对比测试,详细评估了不同插值算法(包括最邻近差值、双线性插值)在图像质量与计算性能上的差异,帮助开发者掌握高质量图像缩放的核心技术。

weixin_29310605的博客 302

On the convergence of FedAvg on non-iid data

On the convergence of FedAvg on non-iid data

Lyn_S的博客 7089

理解 ReadDirectoryChangesW

理解 ReadDirectoryChangesW 原作者:Jim Beveridge 原文:http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html?amp 渣翻译:bbca

求索 1万+

手把手教你用TP4056和PMOS管搭建锂电池自动切换电路(含元器件选型指南)

本文详细介绍了如何使用TP4056和PMOS管搭建锂电池自动切换电路,包括核心架构设计、元器件选型指南和PCB布局优化。通过智能充电算法和电源路径切换模块,实现外部供电与锂电池的无缝切换,提升便携式设备的电源管理效率。

julia4scientist的博客 906

ReadDirectoryChangesW 监控文件变化

本文测试了ReadDirectoryChangesW类监控文件变化。 #include<iostream> #include<string> #include<Windows.h> using namespace std; string WStringToString(const std::wstring &wstr); void R...

万里归来少年心 3861

保姆级教程:用MediaPipe和BlazePose在Python里实时追踪你的健身动作(附完整代码)

本文提供了一份详细的Python教程,教你如何使用MediaPipe和BlazePose实现实时健身动作追踪。从环境配置、模型选型到实时视频流处理架构设计,再到健身动作量化分析和可视化反馈系统优化,教程涵盖了完整的实现步骤和代码示例,帮助开发者快速构建高效的姿态特征点检测系统。

weixin_42531779的博客 157

成功解决使用jupyter notebook命令打开原先存的.ipynb文件突然消失的问题

成功解决使用jupyter notebook命令打开原先存的.ipynb文件突然消失的问题 目录 解决问题 解决思路 解决方法 T1、修改默认配置 T2、修改后缀参数(此方法是其他网友推荐,博主没有尝试) T3、如果以上还不成功,这个时候很可能是环境配置有问题 解决问题 使用jupyter notebook命令打开原先存的.ipynb文件突然消失的问题 解决思路 使用jupyter notebook命令打开原先存的.ipynb文件突然消失,说明...

头部AI社区如有邀博主AI主题演讲请私信—心比天高,仗剑走天涯,保持热爱,奔赴向梦想!低调,专注,谦虚,自律,反思,成长,还算比较正能量的博主,公益免费传播…内心特别想在AI界做出一些可以推进历史进程影响力的技术(兴趣使然,有点小情怀,也有点使命感呀 5125

fswatch在Windows环境部署:ReadDirectoryChangesW API实战指南

想要在Windows系统上实现高效的文件监控吗?fswatch作为一款跨平台的文件系统监控工具,通过ReadDirectoryChangesW API在Windows环境提供了强大的实时文件监控能力。本指南将带你从零开始,完成fswatch在Windows环境的完整部署流程。🚀 ## 什么是fswatch和ReadDirectoryChangesW? fswatch是一个功能强大的跨平台文件

gitblog_00549的博客 1325

hwasan / asan详细分析踩内存之一:Use after free

UAF漏洞全称为use after free ,即利用已经被free掉的堆块被再次利用,该漏洞的存在是因为程序编写者在free堆块部分没有将该堆块的fd指针改为0,进而导致该堆块可以被申请回来,3.内存块被free后,其对应的指针没有被设置为 NULL,但是在它下一次使用之前,有代码对这块内存进行了修改,那么当程序再次使用这块内存时,就很有可能会出现奇怪的问题。2.堆块free后,对应指针没有被设置为NULL,即还可以调用会该堆块,当我们申请一个比该堆块小于等于的size时。

1406

监控目录文件变化的ReadDirectoryChangesW 函数学习总结

客户端监控被监控目录(含所有子目录)下的变化,我们可以采用ReadDirectoryChangesW 函数 ,该函数实现对指定的目录进行监控,并且返回详细的文件变化信息。 函数原型: BOOL WINAPI ReadDirectoryChangesW(   __in         HANDLE hDirectory;   __out        LPVOID lpBuffer;   

peixuan的专栏 2986

基于强化学习控制策略的三相逆变器仿真

强化学习控制器的核心在于定义状态空间、动作空间、奖励函数以及选择合适的强化学习算法(例如DQN、DDPG等)。对于三相逆变器,状态可以是输出电压、电流等信息;动作则可能是调制指数的变化量;奖励函数则应鼓励快速准确地跟踪参考信号。定义状态空间和动作空间状态空间可以包括逆变器的输出电压、电流及其变化率等信息。动作空间则是控制器可执行的动作,例如PWM信号的调整幅度。matlab深色版本% 定义状态和动作空间维度numObs = 6;% 假设有六个状态变量(例如三相电压和电流)

amy_mhd的博客 146

微信小程序 - 最新详细实现用户保存下载视频到手机相册,将视频临时路径、在线播放地址(网络路径)URL存储到手机,弹出访问手机相册权限询问,支持视频格式mp4、avi、mov、wmv、flv、webm

微信小程序,视频,手机相册,保存,另存为,小程序保存到相册权限,writePhotosAlbum,一键保存视频到手机相册功能(视频来源为链接),第一次要获取用户授权,授权使用相册权限,小程序视频下载,如何把视频下载到相册,下载一个视频到本地相册,显示下载进度,可取消下载,视频下载到本地,微信小程序视频如何下载,保存视频到相机,页面播放视频并下载到本地,安卓系统,苹果ios保存到相册报错,权限拒绝后打不开,添加相册授权弹框拒绝后再也出不来了,实现保存视频到手机相册功能,将mp4在线视频下载并存储到用户的手机中

🏆 前端领域优质创作者 7644

python watchdog

最近在整理一些自动化脚本,又用到了watchdog这个库。它不是什么新潮玩意儿,但在处理文件系统监控这类需求时,确实是个顺手的老伙计。今天就来聊聊它,不搞那些教科书式的罗列,就说说平时怎么用它,以及一些容易踩的坑。

leohu0723的博客 430

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

ReadDirectoryChangesW监控目录文件操作

// 目录监控多线程 UINT MonitorFileThreadProc(LPVOID lpVoid) { char *pszDirectory = (char *)lpVoid; // 打开目录, 获取文件句柄 HANDLE hDirectory = ::CreateFile(pszDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP

Cosmopolitan的博客 781

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Windows监控文件变化(ReadDirectoryChangesW)

Windows提供了API对文件和目录进行监控,分别是:FindFirstChangeNotification和ReadDirectoryChangesW。 FindFirstChangeNotification函数,可以监控到目标目录及其子目录中下有文件发生改变,但不能监控到具体是哪一个文件发生改变。 ReadDirectoryChangesW 能监控到目标目录下某一文件发生改变,并

pjl1119的专栏 8757

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

C++ 使用ReadDirectoryChangesW监测文件夹

直接上代码: FileSystemWatcher.h #ifndef __FILESYSTEMWATCHER_HPP__ #define __FILESYSTEMWATCHER_HPP__ #if(_WIN32_WINNT < 0x0400) #define _WIN32_WINNT 0x0400 #endif #include <windows.h> class FileSystemWatcher { public: enum Filter { FILTER_FILE_

technologyleader的专栏 1791

关于ReadDirectoryChangesW的三两事

最近在做云盘,本来的同步方式效率比较低,所以我就想着做个文件夹监视,监控同步的文件夹。所以用到的ReadDirectoryChangesW这个api,虽然网上关于这个api的介绍有不少但是有些细节介绍的还是比较少的所以留下这篇文章给大家一个参考,也给自己留个备份。 首先贴下我的代码: void CFolderMonitor::DoMonitor() { char *buffer

williom90320的专栏 1082
上一篇: RemObjects系列产品介绍
下一篇: 解决Webbrowser加载Flash后,方向键等冲突
dropme
博客等级 码龄24年 16粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值