直接上代码:
#include "stdafx.h"
typedef struct ThreadParameter
{
LPTSTR in_directory;//监控的路径
FILE_NOTIFY_INFORMATION *in_out_notification;//存储监控函数返回信息地址
DWORD in_MemorySize;//传递存储返回信息的内存的字节数
DWORD *in_out_BytesReturned;//存储监控函数返回信息的字节数
FILE_NOTIFY_INFORMATION *temp_notification;//备用的一个参数
}ThreadParameter;
char* w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nlength = wcslen(pwstr);
int nbytes = WideCharToMultiByte(0,0,pwstr,nlength,NULL,0,NULL,NULL );
// make sure the buffer is big enough for this, making it larger if necessary
if(nbytes > len) nbytes = len;
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte(0,0,pwstr,nlength,pcstr,nbytes,NULL,NULL);
return pcstr ;
}
DWORD WINAPI WatchChanges(LPVOID lpParameter)
{
ThreadParameter *parameter = (ThreadParameter*)lpParameter;
LPCTSTR WatchDirectory=parameter->in_directory;//监控目录
//创建一个目录句柄
HANDLE handle_directory = CreateFile(WatchDirectory,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if(handle_directory == INVALID_HANDLE_VALUE)
{
DWORD ERROR_DIR = GetLastError();
MessageBox(NULL,TEXT("打开目录错误!"),TEXT("文件监控"),0);
}
BOOL watch_state;
int dwNextEntryOffset(0);
char szFileName[MAX_PATH] = {0};
while(TRUE)
{
watch_state = ReadDirectoryChangesW(handle_directory,
(LPVOID)parameter->in_out_notification,
parameter->in_MemorySize,
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
(LPDWORD)parameter->in_out_BytesReturned,
NULL,
NULL);
//If the network redirector or the target file system does not support this operation,
//the function fails with ERROR_INVALID_FUNCTION.
if (GetLastError() == ERROR_INVALID_FUNCTION)
{
MessageBox(NULL,TEXT("系统不支持!"),TEXT("文件监控"),0);
}
//If the function fails, the return value is zero (0).
else if(watch_state == 0)
{
MessageBox(NULL,TEXT("监控失败!"),TEXT("文件监控"),0);
}
//If the buffer overflows, the changes are discarded and the function fails with ERROR_NOTIFY_ENUM_DIR.
else if (GetLastError() == ERROR_NOTIFY_ENUM_DIR)
{
MessageBox(NULL,TEXT("内存溢出!"),TEXT("文件监控"),0);
}
else
{
do
{
dwNextEntryOffset = parameter->in_out_notification->NextEntryOffset ;
switch (parameter->in_out_notification->Action)
{
case FILE_ACTION_REMOVED : //文件删除
{
w2c(szFileName,parameter->in_out_notification->FileName,sizeof(szFileName));
printf("删除了一个文件:%s\n",szFileName);
}
break ;
case FILE_ACTION_ADDED : //文件添加
{
w2c(szFileName,parameter->in_out_notification->FileName,sizeof(szFileName));
printf("添加了一个文件:%s\n",szFileName);
}
break;
case FILE_ACTION_MODIFIED : //文件修改
{
w2c(szFileName,parameter->in_out_notification->FileName,sizeof(szFileName));
printf("修改了一个文件:%s\n",szFileName);
}
break ;
case FILE_ACTION_RENAMED_OLD_NAME : //文件改名
{
w2c(szFileName,parameter->in_out_notification->FileName,sizeof(szFileName));
printf("改名了一个文件:%s(OLD_NAME)\n",szFileName);
}
break;
case FILE_ACTION_RENAMED_NEW_NAME: //文件改名:新名字
{
w2c(szFileName,parameter->in_out_notification->FileName,sizeof(szFileName));
printf("改名了一个文件:%s(NEW_NAME)\n",szFileName);
}
break ;
default:
break;
}
if (dwNextEntryOffset != 0)
{
parameter->in_out_notification = (FILE_NOTIFY_INFORMATION*)((BYTE*)parameter->in_out_notification + dwNextEntryOffset);
}
}while(dwNextEntryOffset != 0);
memset(parameter->in_out_notification,'\0',1024);
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
char notify[1024] = {0};
FILE_NOTIFY_INFORMATION *Notification = (FILE_NOTIFY_INFORMATION *)notify;
FILE_NOTIFY_INFORMATION *TempNotification = NULL;
DWORD BytesReturned = 0;
DWORD version = 0;
ThreadParameter ParameterToThread = {"D:\\Data",Notification,sizeof(notify),&BytesReturned,TempNotification};
HANDLE TrheadWatch = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WatchChanges,&ParameterToThread,0,NULL);
CloseHandle(TrheadWatch);
while (TRUE)
{
Sleep(10000);
}
return 0;
}
结果:
删除了一个文件:复件 微信000.lnk
改名了一个文件:微信000.lnk$.lnk(OLD_NAME)
改名了一个文件:微信0001.lnk.lnk(NEW_NAME)
添加了一个文件:新建 文本文档.txt
修改了一个文件:新建 文本文档.txt\
修改了一个文件:新建 文本文档.txt\