Obtaining Directory Change Notifications(微软的例子,使用FindFirstChangeNotification,FindNextChangeNotification,FindCloseChangeNotification API函数)
An application can monitor the contents of a directory and its subdirectories by using change notifications. Waiting for a change notification is similar to having a read operation pending against a directory and, if necessary, its subdirectories. When something changes within the directory being watched, the read operation is completed. For example, an application can use these functions to update a directory listing whenever a file name within the monitored directory changes.
An application can specify a set of conditions that trigger a change notification by using the FindFirstChangeNotification function. The conditions include changes to file names, directory names, attributes, file size, time of last write, and security. This function also returns a handle that can be waited on by using the wait functions. If the wait condition is satisfied, FindNextChangeNotification can be used to provide a notification handle to wait on subsequent changes. However, these functions do not indicate the actual change that satisfied the wait condition.
Use FindCloseChangeNotification to close the notification handle.
To retrieve information about the specific change as part of the notification, use the ReadDirectoryChangesW function. This function also enables you to provide a completion routine.
To track changes on a volume, see change journals.
The following example monitors the directory tree for directory name changes. It also monitors a directory for file name changes. The example uses theFindFirstChangeNotification function to create two notification handles and the WaitForMultipleObjects function to wait on the handles. Whenever a directory is created or deleted in the tree, the example should update the entire directory tree. Whenever a file is created or deleted in the directory, the example should refresh the directory.
This simplistic example uses the ExitProcess function for termination and cleanup, but more complex applications should always use proper resource management such as FindCloseChangeNotification where appropriate.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h> void RefreshDirectory(LPTSTR);
void RefreshTree(LPTSTR);
void WatchDirectory(LPTSTR); void _tmain(int argc, TCHAR *argv[])
{
if(argc != 2)
{
_tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
return;
} WatchDirectory(argv[1]);
} void WatchDirectory(LPTSTR lpDir)
{
DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];
TCHAR lpDrive[4];
TCHAR lpFile[_MAX_FNAME];
TCHAR lpExt[_MAX_EXT]; _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT); lpDrive[2] = (TCHAR)'\\';
lpDrive[3] = (TCHAR)'\0'; // Watch the directory for file creation and deletion. dwChangeHandles[0] = FindFirstChangeNotification(
lpDir, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
{
printf("\n ERROR: FindFirstChangeNotification function failed.\n");
ExitProcess(GetLastError());
} // Watch the subtree for directory creation and deletion. dwChangeHandles[1] = FindFirstChangeNotification(
lpDrive, // directory to watch
TRUE, // watch the subtree
FILE_NOTIFY_CHANGE_DIR_NAME); // watch dir name changes if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
{
printf("\n ERROR: FindFirstChangeNotification function failed.\n");
ExitProcess(GetLastError());
} // Make a final validation check on our handles. if ((dwChangeHandles[0] == NULL) || (dwChangeHandles[1] == NULL))
{
printf("\n ERROR: Unexpected NULL from FindFirstChangeNotification.\n");
ExitProcess(GetLastError());
} // Change notification is set. Now wait on both notification
// handles and refresh accordingly. while (TRUE)
{
// Wait for notification. printf("\nWaiting for notification...\n"); dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,
FALSE, INFINITE); switch (dwWaitStatus)
{
case WAIT_OBJECT_0: // A file was created, renamed, or deleted in the directory.
// Refresh this directory and restart the notification. RefreshDirectory(lpDir);
if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE )
{
printf("\n ERROR: FindNextChangeNotification function failed.\n");
ExitProcess(GetLastError());
}
break; case WAIT_OBJECT_0 + 1: // A directory was created, renamed, or deleted.
// Refresh the tree and restart the notification. RefreshTree(lpDrive);
if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE )
{
printf("\n ERROR: FindNextChangeNotification function failed.\n");
ExitProcess(GetLastError());
}
break; case WAIT_TIMEOUT: // A timeout occurred, this would happen if some value other
// than INFINITE is used in the Wait call and no changes occur.
// In a single-threaded environment you might not want an
// INFINITE wait. printf("\nNo changes in the timeout period.\n");
break; default:
printf("\n ERROR: Unhandled dwWaitStatus.\n");
ExitProcess(GetLastError());
break;
}
}
} void RefreshDirectory(LPTSTR lpDir)
{
// This is where you might place code to refresh your
// directory listing, but not the subtree because it
// would not be necessary. _tprintf(TEXT("Directory (%s) changed.\n"), lpDir);
} void RefreshTree(LPTSTR lpDrive)
{
// This is where you might place code to refresh your
// directory listing, including the subtree. _tprintf(TEXT("Directory tree (%s) changed.\n"), lpDrive);
}
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365261(v=vs.85).aspx
Obtaining Directory Change Notifications(微软的例子,使用FindFirstChangeNotification,FindNextChangeNotification,FindCloseChangeNotification API函数)的更多相关文章
- [WPF系列]基础 Listening to Dependency Property change notifications of a given Element
I want to share this great post from Anoop that shows a easy way to add a notification system to dep ...
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- 浅议Delphi中的Windows API调用(举的两个例子分别是String和API,都不错,挺具有代表性)
浅议Delphi中的Windows API调用http://tech.163.com/school • 2005-08-15 10:57:41 • 来源: 天极网为了能在Windows下快速开发应用程 ...
- 为微软ContosoUniversity例子加上学生自选课程计划
把ContosoUniversity例子过了一遍,好象还是有很多东西未能理解,决定自己随便加个功能看可以自己完成不.... 从github的例子中clone下来ContosoUniversity项目, ...
- 孤陋寡闻又一遭:ReportEvent API函数(有微软Service官方例子为例)
API 详解: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363679(v=vs.85).aspx 使用例子: https: ...
- 微软BI 之SSRS 系列 - 巧用 RunningValue 函数在分组中排序并设置 RANK 排名
开篇介绍 经常有像类似于这样的排序需求,以及设置分组下的排序序号.比如此图中要求城市 City 在省份下按照 Internet Sales Amount 总销售额进行排序,并标识在各省份下的排名. 实 ...
- SetTimer and CreateWaitableTimer的例子(静态函数设置为回调函数,瑞士的网页,有点意思)
Timers (SetTimer and CreateWaitableTimer) in Windows SetTimer The following example creates a time ...
- 一个简单例子弄懂什么是javascript函数劫持
javascript函数劫持很简单,一般情况下,只要在目标函数触发之前,重写这个函数即可. 比如,劫持eval函数的代码如下: var _eval=eval; eval=function(x){ if ...
- live555例子程序编译连接时发现函数未定义问题
1 调整连接库的顺序. 2 更新头文件与所用的库一致
随机推荐
- 嵌入式Qt-4.8.6显示中文并且改变字体大小和应用自己制作的字体库
问题: QT4.8.6在移植到开发板上的时候,中文支持是必不可少的,如何让QT支持中文,如何制作QT支持的字体文件,如何使QT UI编辑器中的字号与开发板中的字号一致. 详解: 1>如何让QT支 ...
- javascript进阶课程--第三章--匿名函数和闭包
javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...
- mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure
err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...
- testng并发测试与测试并发
一.testng并发测试 通过xml文件中suit结点的parallel属性指定,如 <suite name="bundle-module-testabc" parallel ...
- iOS开发网络学习七:NSURLSession的基本使用get和post请求
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- 事件处理之一:两种方式:监听器与回调 分类: H1_ANDROID 2013-10-31 10:26 3250人阅读 评论(0) 收藏
Android组件的事件处理有2种方式: 1.基于监听器的事件处理方式:先定义组件,然后为组件设定监听器. 详见http://blog.csdn.net/jediael_lu/article/deta ...
- [转载]Ocelot简易教程(四)之请求聚合以及服务发现
上篇文章给大家讲解了Ocelot的一些特性并对路由进行了详细的介绍,今天呢就大家一起来学习下Ocelot的请求聚合以及服务发现功能.希望能对大家有所帮助. 作者:依乐祝 原文地址:https://ww ...
- 【9201】&&【a201】明明的随机数
Time Limit: 10 second Memory Limit: 2 MB 问题描述 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整 ...
- USB 3.0规范中译本 第4章 超高速数据流模型
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章展示数据和信息如何在超高速上通过的一种高层次的描述.请阅读协议层一章关于低层次协议的细节.本章提供设备架 ...
- js对时间的操作相关
摘自网络,我主要用了日期增加若干天之后获得的日期,就是现在是5月2号,我增加30天,应该返回6月几号来着,就是这个意思 用到了Date.prototype.DateAdd 方法,prototype的意 ...