#include <windows.h>
#include "resource.h"
//自定义监视文件的线程函数
int MonitorFile()
{
DWORD dwStatus;
HANDLE hHandle;
//监控指定文件夹
hHandle = FindFirstChangeNotification(
"C://11",FALSE,FILE_NOTIFY_CHANGE_FILE_NAME);
if(hHandle != INVALID_HANDLE_VALUE)
{
while(1)
{
dwStatus = WaitForSingleObject(hHandle,INFINITE);
if(dwStatus == WAIT_OBJECT_0)
{
MessageBox(NULL,"目录里的文件有变动,请注意","提示",MB_OK);
FindNextChangeNotification(hHandle);//监控下个文件
}
}
}
FindCloseChangeNotification(hHandle);
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
if (uMsg == WM_CLOSE)
{
EndDialog(hwnd,0);
}
else if(uMsg == WM_COMMAND)
{
if(wParam == IDC_BUTTON1)
{
DWORD id;
//创建监视文件的线程
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)MonitorFile,NULL,0,&id);
}
}
return FALSE;
}
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,&WindowProc,0);
}
FindFirstChangeNotification FindNextChangeNotification函数详细信息见MSDN。。。