监视文件夹的action[文件增加,修改,删除,重命名等]
Posted zeqi1991
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监视文件夹的action[文件增加,修改,删除,重命名等]相关的知识,希望对你有一定的参考价值。
void WatchFile()
DWORD cbBytes = 0;
char file_name[MAX_PATH] = 0;
char notify[1024] = 0;
int count = 0;
TCHAR* dir = _T("D:\\\\");
std::string s = "D:";
HANDLE dirHandle = ::CreateFile(dir, GENERIC_READ | GENERIC_WRITE | FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (dirHandle == INVALID_HANDLE_VALUE)
return;
memset(notify, 0, strlen(notify));
FILE_NOTIFY_INFORMATION* pnotify = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(notify);
while (true)
if (::ReadDirectoryChangesW(dirHandle, ¬ify, 1024, true,
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE,
&cbBytes, NULL, NULL))
pnotify = (FILE_NOTIFY_INFORMATION*)notify;
//转换文件名为多字节字符串;
if (pnotify != nullptr && pnotify->FileName != nullptr)
ZeroMemory(file_name, sizeof(char) * MAX_PATH);
int ret = ::WideCharToMultiByte(CP_ACP, 0, pnotify->FileName, pnotify->FileNameLength / 2, file_name, 99, NULL, NULL);
if (ret == 0)
DWORD err = ::GetLastError();
if ((pnotify->Action == FILE_ACTION_ADDED) ||
(pnotify->Action == FILE_ACTION_REMOVED) ||
(pnotify->Action == FILE_ACTION_RENAMED_OLD_NAME) ||
(pnotify->Action == FILE_ACTION_MODIFIED))
//如果发生了这些事件的话需要重新扫描当前的目录文件
//就需要获取当前的目录名称
std::string dirpath = s + "\\\\" + file_name;
::CloseHandle(dirHandle);
int main(int argc, char* argv[])
WatchFile();
return 0;
以上是关于监视文件夹的action[文件增加,修改,删除,重命名等]的主要内容,如果未能解决你的问题,请参考以下文章