使用vc++ 怎么監(jiān)控指定路徑下文件變化?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
成都創(chuàng)新互聯(lián)主營(yíng)邯鄲網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),邯鄲h5微信小程序定制開(kāi)發(fā)搭建,邯鄲網(wǎng)站營(yíng)銷推廣歡迎邯鄲等地區(qū)企業(yè)咨詢
具體看代碼
# include < iostream > # include < windows.h > # include < process.h > # include < tchar.h > # include < string > using namespace std; typedef void( * CHANGESCALLBACK)(const std::wstring strFileName); /* @ 監(jiān)控指定目錄下文件變化 @ strDirPath 需要監(jiān)控的目錄 @ dwChangesType 需要監(jiān)控文件變化方式 @ onCallBack 變化后的回調(diào)處理函數(shù) */ void CheckFilesChanges(const std::wstring strDirPath, DWORD dwChangesType, CHANGESCALLBACK onCallBack); void PrintMsg(const std::wstring strFileName) { wprintf_s(L "new add File:\t %s\r\n", strFileName.c_str()); } void CheckFilesChanges(const std::wstring strDirPath, DWORD dwChangesType, CHANGESCALLBACK onCallBack) { if (strDirPath.empty() || !onCallBack) return; HANDLE hDir = INVALID_HANDLE_VALUE; BYTE lpBuffer[1024]; ZeroMemory(lpBuffer, 1024); DWORD cbBytes = NULL; BOOL isOk = FALSE; FILE_NOTIFY_INFORMATION * pnotify = (FILE_NOTIFY_INFORMATION * )lpBuffer; FILE_NOTIFY_INFORMATION * tmp; ZeroMemory( & lpBuffer, sizeof(FILE_NOTIFY_INFORMATION)); hDir = CreateFile(strDirPath.c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL); if (INVALID_HANDLE_VALUE == hDir) return; while (true) { isOk = ReadDirectoryChangesW(hDir, & lpBuffer, sizeof(lpBuffer), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, & cbBytes, NULL, NULL); if (isOk) { tmp = pnotify; PWSTR fileName = nullptr; std::wstring strFileName; if (tmp->FileNameLength) { DWORD dwLength = tmp->FileNameLength + 1; fileName = new wchar_t[dwLength]; if (fileName) { memset(fileName, 0, dwLength * 2); memcpy(fileName, tmp->FileName, dwLength * 2); strFileName = fileName; } if (fileName) delete []fileName; } if (tmp->Action == dwChangesType) { onCallBack(strFileName); } ZeroMemory(tmp, 1024); /*switch (tmp->Action){ case FILE_ACTION_ADDED: //當(dāng)前目錄新增文件{ onCallBack(strFileName); }break; case FILE_ACTION_REMOVED: //該文件已從目錄中刪除{ onCallBack(strFileName); } break; case FILE_ACTION_MODIFIED: //該文件已被修改。也可以是時(shí)間戳或?qū)傩缘母膡 onCallBack(strFileName); } break; case FILE_ACTION_RENAMED_OLD_NAME: //該文件已重命名,這是舊名稱。{ onCallBack(strFileName); } break; case FILE_ACTION_RENAMED_NEW_NAME: //該文件已重命名,這是新名稱。{ onCallBack(strFileName); } break; default: break; }*/ } } CloseHandle(hDir); } unsigned int CALLBACK ThreadProc(void * arg) { CheckFilesChanges(_T("E:\\GoCode"), FILE_ACTION_ADDED, PrintMsg); return 0; } int main(int argc, char * argv[]) { HANDLE hThread = NULL; hThread = (HANDLE)::_beginthreadex(nullptr, NULL, ThreadProc, NULL, NULL, NULL); system("pause"); return 0; }
關(guān)于使用vc++ 怎么監(jiān)控指定路徑下文件變化問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。