FindFirstChangeNotification 锁定父文件夹
FindFirstChangeNotification locks parent folder
我正在使用 FindFirstChangeNotification()/ReadDirectoryChangesW() 来监视文件夹的更改。它按预期工作。我的问题是:当我尝试重命名监视文件夹的父文件夹时,访问被拒绝,错误 5。我猜我的 FCN 句柄打开了文件夹以进行通知监控,并且不允许更改父级。
有没有办法监视子目录,比如 c:\folder\subfolder\otherfolder\,并且仍然能够打开命令提示符(或其他程序)并执行"重命名 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
}
}
不确定这是否是你需要的,
如果您有兴趣在 c:\folder\subfolder\otherfolder\ 中维护当前侦听器的同时侦听另一个路径,您将需要一个HANDLE
类型的数组。
在您的代码中,它将如下所示:
HANDLE m_hChangeHandle[x];// int x should be the quantity of your desired listeners.
当然,您需要为每个数组使用 FindFirstChangeNotification
,即:
m_hChangeHandle[0] = FindFirstChangeNotification(..., ..., ...);
m_hChangeHandle[1] = FindFirstChangeNotification(..., ..., ...);
//and so on..
由于您现在使用超过 1 个侦听器,因此您不能继续使用 WaitForSingleObject
而是使用 WaitForMultipleObjects。 即:
WaitForMultipleObjects(x, m_hChangeHandle, FALSE, INFINITE);//False will wait for a change in at least one of the listeners.
希望这是你需要的。
相关文章:
- 如何将更多文件夹添加到c++include路径
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何在文档文件夹中创建目录
- "assimp/config.h"找不到,但它在文件夹中
- C++入门 5 版:类消息和文件夹
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 如何按文件扩展名引用文件夹中的文件
- 在QTreeView中仅显示共享驱动器和文件夹
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 介子复制/安装头文件到输出目录并保持文件夹结构
- vcruntime.h 从 Windows 包含文件夹中丢失?
- 在文件夹迭代上实现 RAII
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 无法使用 CMake 从其他文件夹添加源文件
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 我可以在C 中的Windows中读取一个文件,而无需锁定包含文件的文件夹
- FindFirstChangeNotification 锁定父文件夹