返回> 网站首页 

监视操作系统文件

yoours2010-05-15 15:36:27 阅读 1211

简介一边听听音乐,一边写写文章。

FindFirstChangeNotification函数创建一个更改通知句柄并设置初始更改通知过滤条件. 当一个在指定目录或子目录下发生的更改符合过滤条件时,等待通知句柄则成功。 
该函数原型为:
HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName, //目录名
BOOL bWatchSubtree, // 监视选项
DWORD dwNotifyFilter // 过滤条件
);


当下列情况之一发生时,WaitForMultipleObjects函数返回
1.一个或者全部指定的对象在信号状态(signaled state)
2.到达超时间隔


例程如下:
DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];
//监视C:\Windows目录下的文件创建和删除
dwChangeHandles[0] = FindFirstChangeNotification(
"C:\\WINDOWS", // directory to watch
FALSE, // do not watch the subtree
FILE_NOTIFY_CHANGE_FILE_NAME);// watch file name changes


if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());


//监视C:\下子目录树的文件创建和删除
dwChangeHandles[1] = FindFirstChangeNotification(
"C:\\", // directory to watch
TRUE, // watch the subtree
FILE_NOTIFY_CHANGE_DIR_NAME);// watch dir. name changes


if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());


// Change notification is set. Now wait on both notification 
// handles and refresh accordingly
while (TRUE)
{
// Wait for notification
dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,FALSE, INFINITE);
switch (dwWaitStatus)
{
case WAIT_OBJECT_0:
//在C:\WINDOWS目录中创建或删除文件
//刷新该目录及重启更改通知(change notification)
AfxMessageBox("RefreshDirectory");
if ( FindNextChangeNotification(dwChangeHandles[0])== FALSE)
ExitProcess(GetLastError());
break;


case WAIT_OBJECT_0 1:
//在C:\WINDOWS目录中创建或删除文件 。 
//刷新该目录树及重启更改通知(change notification)
AfxMessageBox("RefreshTree");
if (FindNextChangeNotification(dwChangeHandles[1])== FALSE)
ExitProcess(GetLastError());
break;


default:
ExitProcess(GetLastError());
}
}
微信小程序扫码登陆

文章评论

1211人参与,0条评论