我正在使用 FindFirstChangeNotification()/ReadDirectoryChangesW() 来监视文件夹的更改。它按预期工作。我的问题是:当我尝试重命名 Watched 文件夹的父级时,我被拒绝访问,错误 5。我猜我的 FCN 句柄已打开文件夹以进行通知监视,并且不允许更改父级。
有什么方法可以监视子目录,比如 c:\folder\subfolder\anotherfolder\ 并且仍然能够打开命令提示符(或其他程序)并执行“重命名 c:\folder\c:\folder2”
这是我的代码片段
//
// Sign up for notifications on the object.
//
m_bWatchSubTree = TRUE;
m_dwWatchFlags =
FILE_NOTIFY_CHANGE_SECURITY | // (0x100) change to security descriptor (NTFSACL?)
FILE_NOTIFY_CHANGE_CREATION | // (0x40) change to creation date
FILE_NOTIFY_CHANGE_LAST_ACCESS | // (0x20) change to last access date
FILE_NOTIFY_CHANGE_LAST_WRITE | // (0x10) any change to the modification date/time of file in the folder
FILE_NOTIFY_CHANGE_SIZE | // (0x08) Size changed
FILE_NOTIFY_CHANGE_ATTRIBUTES | // (0x04) Atributes of something changed
FILE_NOTIFY_CHANGE_DIR_NAME | // (0x02) DIR name change in watched folder rename,create,delete FOLDER
FILE_NOTIFY_CHANGE_FILE_NAME; // (0x01) FILE name change in watched folder. rename,create,delete FILE
CString strObjectPathAndName(_T("c:\\folder\\subfolder\\anotherfolder\\");
m_hChangeHandle = ::FindFirstChangeNotification(strObjectPathAndName, m_bWatchSubTree, m_dwWatchFlags);
if (m_hChangeHandle != INVALID_HANDLE_VALUE)
{
DWORD dwStatus = ::WaitForSingleObject(m_hChangeHandle, INFINITE);
HANDLE hDir = ::CreateFile(strObjectPathAndName,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE| FILE_SHARE_DELETE,
NULL, //security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL);
DWORD dwBytesReturned = 0;
FILE_NOTIFY_INFORMATION *p = (FILE_NOTIFY_INFORMATION*)nb.GetBuffer();
if (::ReadDirectoryChangesW(hDir, p, nb.GetMaxSize(), m_bWatchSubTree, m_dwWatchFlags, &dwBytesReturned, NULL, NULL))
{
//
// do some stuff
}
}