问题
I'm using FindFirstChangeNotification()/ReadDirectoryChangesW() to watch a folder for changes. It's working as expected. My Question is: When I try to rename the parent of the Watched folder, I get access denied, error 5. I'm guessing my FCN handle has the folder open for notification monitoring and changing the parent is not allowed.
Is there any way to monitor a subdirectory, say c:\folder\subfolder\anotherfolder\ and still be able to open a command prompt (or other program) and execute a "rename c:\folder\ c:\folder2"
here's my code snippet
//
// 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
}
}
回答1:
Not sure if that's what you need,
If you are interested in listening to ANOTHER path while maintaining your current listener in c:\folder\subfolder\anotherfolder\, you will need an array of HANDLE
types.
In your code it will look like this:
HANDLE m_hChangeHandle[x];// int x should be the quantity of your desired listeners.
Of course you need to use FindFirstChangeNotification
for each of your array, ie:
m_hChangeHandle[0] = FindFirstChangeNotification(..., ..., ...);
m_hChangeHandle[1] = FindFirstChangeNotification(..., ..., ...);
//and so on..
Since you are using now more then 1 listener you can't keep using WaitForSingleObject
but rather WaitForMultipleObjects. i.e:
WaitForMultipleObjects(x, m_hChangeHandle, FALSE, INFINITE);//False will wait for a change in at least one of the listeners.
Hope it's what you needed.
来源:https://stackoverflow.com/questions/27962294/findfirstchangenotification-locks-parent-folder