C++ ReadDirectoryChangesW 和 Boost 将目录更改作为文件(旧名称)返回
C++ ReadDirectoryChangesW and Boost returns dir change as File (OLD NAME)
我有一个应用程序正在监视目录的更改。但是,当我重命名目录时,假设目录 A 进入目录 B,那么我将看到以下输出结果:
File renamed (OLD): C:A
Directory renamed (NEW): C:B
虽然我期望输出:
Directory renamed (OLD): C:A
Directory renamed (NEW): C:B
这种情况也发生在删除目录时,给出输出:
File removed: C:Atest.txt
File modified: C:A
File removed: C:A
虽然我期望输出:
File removed: C:Atest.txt
Directory modified: C:A
Directory removed: C:A
我用来获取此输出的代码(使用 Boost 的文件系统(是:
while(true) {
FILE_NOTIFY_INFORMATION* info = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(p);
int ret = ::WideCharToMultiByte(CP_ACP, 0, info->FileName, info->FileNameLength / sizeof(WCHAR), FilePathChar, sizeof(FilePathChar), NULL, NULL);
stringstream FilePathStream;
FilePathStream << argv[1];
FilePathStream << "\";
FilePathStream << FilePathChar;
string FilePath = FilePathStream.str();
cout << FilePath << endl;
boost::filesystem::path path(FilePath);
string Type = "File";
if (boost::filesystem::is_directory(path)) {
Type = "Directory";
}
ofstream myfile;
myfile.open("changes.txt", std::ios_base::app);
switch (info->Action) {
case FILE_ACTION_ADDED:
myfile << Type << " added: " << FilePath << "n";
break;
case FILE_ACTION_MODIFIED:
myfile << Type << " modified: " << FilePath << "n";
break;
case FILE_ACTION_REMOVED:
myfile << Type << " removed: " << FilePath << "n";
break;
case FILE_ACTION_RENAMED_NEW_NAME:
myfile << Type << " renamed (NEW): " << FilePath << "n";
break;
case FILE_ACTION_RENAMED_OLD_NAME:
myfile << Type << " renamed (OLD): " << FilePath << "n";
break;
default:
myfile << Type << " UNDISCOVERED ACTION: " << FilePath << "n";
break;
}
myfile.close();
::memset(FilePathChar, ' ', sizeof(FilePathChar));
if (!info->NextEntryOffset) break;
p += info->NextEntryOffset;
}
我在这里做错了什么?
问题是,在这两种情况下,您都不会收到通知,直到目录不再被调用C:A
(因为它已被删除或移动(。 所以当你去的时候:
if (boost::filesystem::is_directory(path)) {
Type = "Directory";
}
is_directory
返回false
(因此您将Type
保留为"File"
(
我建议改为:
const std::string Type = boost::filesystem::is_directory(path) ? "Directory" :
boost::filesystem::is_regular_file(path) ? "File" :
"unknown";
相关文章:
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- AcquireCredentialsHandleA() 返回 PFX 文件的0x8009030e(安全包中没有可用的凭据
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 为什么生成文件返回 g++: 错误: h 文件或目录 make: *** [生成文件:12: 测试] 错误 1?
- 将单独的头文件和类定义文件链接到主函数文件 - G++ 返回重载"undefined reference to"构造函数
- tellg() 只对小文件返回 -1
- 使用关键字从文本文件返回数字
- 读/写二进制文件返回0xcccccccccccc
- QMediaPlaylist::addMedia() 对于不存在的文件返回 true
- QFileInfo::isExecutable() 为".exe"文件返回 false
- C++ 头文件返回对类型
- 类对象的向量C++文件返回程序
- C++ 搜索 TXT 文件返回 int.
- C 编程 - 查找第一个文件返回Invalid_handle_value
- 从文本文件返回输出的函数
- 从c++ MEX文件返回值到MATLAB
- C++自定义读取函数为文件夹中的前两个文件返回错误的值
- GetFileAttributes为不存在的文件返回FILE_ATTRIBUTE_ARCHIVE
- 使用c++从xml文件返回char*以外的数据类型时出错
- 为什么创建文件返回无效句柄