您的位置:首页 > 其它

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());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: