对于某个系统某个目录变化如何进行监视。监视包含文件的添加、修改、重命名和删除等。在VC驿站上看到的,现在写成blog发表于大家分享。
- //监视线程
- DWORD WINAPI C***Dlg::ThreadWatchProc( LPVOID lParam )
- {
- C***Dlg *obj = (C***Dlg*)lParam;
- obj->m_hDirectory = CreateFile( //打开目录,得到目录的句柄
- obj->m_strWatchedDir,
- GENERIC_READ|GENERIC_WRITE,
- FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
- NULL,
- OPEN_EXISTING,
- FILE_FLAG_BACKUP_SEMANTICS,
- NULL
- );
- if(obj->m_hDirectory ==INVALID_HANDLE_VALUE)
- return false;
- char buf[(sizeof(FILE_NOTIFY_INFORMATION)+MAX_PATH)*2];
- FILE_NOTIFY_INFORMATION* pNotify=(FILE_NOTIFY_INFORMATION*)buf;
- DWORD dwBytesReturned;
- while(true)
- {
- if(::ReadDirectoryChangesW(obj->m_hDirectory,
- pNotify,
- sizeof(buf),
- true,
- FILE_NOTIFY_CHANGE_FILE_NAME|
- FILE_NOTIFY_CHANGE_DIR_NAME|
- FILE_NOTIFY_CHANGE_ATTRIBUTES|
- FILE_NOTIFY_CHANGE_SIZE|
- FILE_NOTIFY_CHANGE_LAST_WRITE|
- FILE_NOTIFY_CHANGE_LAST_ACCESS|
- FILE_NOTIFY_CHANGE_CREATION|
- FILE_NOTIFY_CHANGE_SECURITY,
- &dwBytesReturned,
- NULL,
- NULL))
- {
- char tmp[MAX_PATH], str1[MAX_PATH], str2[MAX_PATH];
- memset(tmp, 0, sizeof(tmp));
- memset(str1,0, sizeof(str1));
- memset(str2,0, sizeof(str2));
- WideCharToMultiByte(CP_ACP,0,pNotify->FileName,pNotify->FileNameLength/2,tmp,99,NULL,NULL);
- strcpy(str1, tmp);
- if(pNotify->NextEntryOffset != 0)
- {
- PFILE_NOTIFY_INFORMATION p = (PFILE_NOTIFY_INFORMATION)((char*)pNotify+pNotify->NextEntryOffset);
- memset(tmp, 0, sizeof(tmp));
- WideCharToMultiByte(CP_ACP,0,p->FileName,p->FileNameLength/2,tmp,99,NULL,NULL);
- strcpy(str2, tmp);
- }
- switch(pNotify->Action)
- {
- case FILE_ACTION_ADDED:
- if(obj->m_bAddNew)
- {
- CTime tt = CTime::GetCurrentTime();
- CString strTT;
- strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond());
- obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex);
- obj->m_listAllInfo.SetItemText(0,2,_T("添加了新文件"));
- obj->m_listAllInfo.SetItemText(0,3,str1);
- obj->m_listAllInfo.SetItemText(0,1,strTT);
- pCheckInfo PI = NULL;
- PI = new tagCheckInfo();
- strcpy(PI->szIndex,obj->m_pszListIndex);
- strcpy(PI->szTime,strTT);
- strcpy(PI->szType,_T("添加了新文件"));
- strcpy(PI->szDetail,str1);
- obj->m_vecCI.push_back(PI);
- }
- break;
- case FILE_ACTION_REMOVED:
- if(obj->m_bDelete)
- {
- CTime tt=CTime::GetCurrentTime();
- CString strTT;
- strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond());
- obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex);
- obj->m_listAllInfo.SetItemText(0,2,_T("删除了文件"));
- obj->m_listAllInfo.SetItemText(0,3,str1);
- obj->m_listAllInfo.SetItemText(0,1,strTT);
- pCheckInfo PI = NULL;
- PI = new tagCheckInfo();
- strcpy(PI->szIndex,obj->m_pszListIndex);
- strcpy(PI->szTime,strTT);
- strcpy(PI->szType,_T("删除了文件"));
- strcpy(PI->szDetail,str1);
- obj->m_vecCI.push_back(PI);
- }
- break;
- case FILE_ACTION_RENAMED_NEW_NAME:
- if(obj->m_bRename)
- {
- CTime tt = CTime::GetCurrentTime();
- CString strTT;
- strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond());
- obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex);
- obj->m_listAllInfo.SetItemText(0,2,_T("重命名了文件"));
- strcat(str1,_T("->"));
- obj->m_listAllInfo.SetItemText(0,3,strcat(str1,str2));
- obj->m_listAllInfo.SetItemText(0,1,strTT);
- pCheckInfo PI = NULL;
- PI = new tagCheckInfo();
- strcpy(PI->szIndex,obj->m_pszListIndex);
- strcpy(PI->szTime,strTT);
- strcpy(PI->szType,_T("重命名了文件"));
- strcpy(PI->szDetail,strcat(str1,str2));
- obj->m_vecCI.push_back(PI);
- }
- break;
- case FILE_ACTION_RENAMED_OLD_NAME:
- if(obj->m_bRename)
- {
- CTime tt=CTime::GetCurrentTime();
- CString strTT;
- strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond());
- obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex);
- obj->m_listAllInfo.SetItemText(0,2,_T("重命名了文件"));
- strcat(str1,_T(" 改名为 "));
- obj->m_listAllInfo.SetItemText(0,3,strcat(str1,str2));
- obj->m_listAllInfo.SetItemText(0,1,strTT);
- pCheckInfo PI = NULL;
- PI = new tagCheckInfo();
- strcpy(PI->szIndex,obj->m_pszListIndex);
- strcpy(PI->szTime,strTT);
- strcpy(PI->szType,_T("重命名了文件"));
- strcpy(PI->szDetail,strcat(str1,str2));
- obj->m_vecCI.push_back(PI);
- }
- break;
- case FILE_ACTION_MODIFIED:
- if(obj->m_bModify)
- {
- CTime tt = CTime::GetCurrentTime();
- CString strTT;
- strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond());
- obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex);
- obj->m_listAllInfo.SetItemText(0,2,_T("修改了文件"));
- obj->m_listAllInfo.SetItemText(0,3,str1);
- obj->m_listAllInfo.SetItemText(0,1,strTT);
- pCheckInfo PI = NULL;
- PI = new tagCheckInfo();
- strcpy(PI->szIndex,obj->m_pszListIndex);
- strcpy(PI->szTime,strTT);
- strcpy(PI->szType,_T("修改了文件"));
- strcpy(PI->szDetail,str1);
- obj->m_vecCI.push_back(PI);
- }
- break;
- default:
- if(obj->m_bOther)
- {
- CTime tt = CTime::GetCurrentTime();
- CString strTT;
- strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond());
- obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex);
- obj->m_listAllInfo.SetItemText(0,2,_T("未知变化"));
- obj->m_listAllInfo.SetItemText(0,3,_T(""));
- obj->m_listAllInfo.SetItemText(0,1,strTT);
- pCheckInfo PI = NULL;
- PI = new tagCheckInfo();
- strcpy(PI->szIndex,obj->m_pszListIndex);
- strcpy(PI->szTime,strTT);
- strcpy(PI->szType,_T("未知变化"));
- strcpy(PI->szDetail,_T(""));
- obj->m_vecCI.push_back(PI);
- }
- break;
- }
- obj->m_nListCount++;
- itoa(obj->m_nListCount,obj->m_pszListIndex,10);
- }
- else
- break;
- }
- obj->m_nListCount = 1;
- return 0;
- }