MFC--监视你的文件系统
2007-04-23 18:05
357 查看
把下面的代码放在线程中处理
void WatchDirectory(LPTSTR lpDir)
{
DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];
TCHAR lpDrive[4];
TCHAR lpFile[_MAX_FNAME];
TCHAR lpExt[_MAX_EXT];
_tsplitpath(lpDir, lpDrive, NULL, lpFile, lpExt);
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)
ExitProcess(GetLastError()); //这里最好利用C/C++的退出线程的代码
// 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)
ExitProcess(GetLastError()); //这里最好利用C/C++的退出线程的代码
// 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:
// A file was created or deleted in the directory.
// Refresh this directory and restart the notification.
---------------PostMessage() 到其它窗口来处理,比如写入日志文件
if ( FindNextChangeNotification(
dwChangeHandles[0]) == FALSE )
ExitProcess(GetLastError());
break;
case WAIT_OBJECT_0 + 1:
// A directory was created or deleted in the subtree.
// Refresh the tree and restart the notification.
---------------PostMessage() 到其它窗口来处理,比如写入日志文件
if (FindNextChangeNotification(
dwChangeHandles[1]) == FALSE)
ExitProcess(GetLastError());
break;
default:
ExitProcess(GetLastError());
}
}
}
void WatchDirectory(LPTSTR lpDir)
{
DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];
TCHAR lpDrive[4];
TCHAR lpFile[_MAX_FNAME];
TCHAR lpExt[_MAX_EXT];
_tsplitpath(lpDir, lpDrive, NULL, lpFile, lpExt);
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)
ExitProcess(GetLastError()); //这里最好利用C/C++的退出线程的代码
// 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)
ExitProcess(GetLastError()); //这里最好利用C/C++的退出线程的代码
// 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:
// A file was created or deleted in the directory.
// Refresh this directory and restart the notification.
---------------PostMessage() 到其它窗口来处理,比如写入日志文件
if ( FindNextChangeNotification(
dwChangeHandles[0]) == FALSE )
ExitProcess(GetLastError());
break;
case WAIT_OBJECT_0 + 1:
// A directory was created or deleted in the subtree.
// Refresh the tree and restart the notification.
---------------PostMessage() 到其它窗口来处理,比如写入日志文件
if (FindNextChangeNotification(
dwChangeHandles[1]) == FALSE)
ExitProcess(GetLastError());
break;
default:
ExitProcess(GetLastError());
}
}
}
相关文章推荐
- 微软的进程监视器--监视进程注册表,文件系统和线程等活动-process monitor
- Python中用MacFSEvents模块监视MacOS文件系统改变一例
- MFC - 打开系统文件对话框
- win7 系统64位 在ivisual studio 2010下运行mfc单文档项目时生成失败出现“fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”的解决办法
- 最近刚为公司完成的一款监视的小工具软件!用DELPHI 7 写的,可以从后台监控系统﹑键盘﹑鼠标﹑屏幕以及文件与目录!有源码!
- 如何调用MFC中的函数打开一个系统资源面板,获取文件
- linux 监视文件系统inotify 测试
- MFC学习笔记 — XP系统写文件失败问题
- MFC点击按钮跳出系统自带的文件选择框
- FSEventStreamCreate监视文件系统改变通知
- LINUX系统监视――常用log日志文件以及各文件说明
- QTFilesystemwatcher文件监视系统
- ASP.NET----监视文件系统
- FindFirstChangeNotification,创建一个文件通知对象,该对象用于监视文件系统发生的变化
- c#使用filesystemwatcher监视文件系统的变化
- Filemon(Filemon文件系统监视)V7.04官方版
- 文件过滤驱动的开发的文件系统监视问题
- 系统文件操作监视功能
- LINUX系统监视——常用log日志文件以及各文件说明
- LINUX系统监视——/proc文件系统(文件夹)