我正在尝试列出 C:\Windows\System32\config
目录中的文件。
我试过像这样使用QDir::entryList()
QDir dir(R"(C:\Windows\System32\config)");
dir.setFilter(QDir::Hidden | QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot);
qDebug().noquote() << dir.entryInfoList();
我也试过像这样使用 std::filesystem::directory_iterator
std::string path = R"(C:\Windows\System32\config)";
for (const auto& entry : std::filesystem::directory_iterator(path))
{
qDebug().noquote() << entry.path().string().c_str();
}
两者都给我相同的输出:
C:\Windows\System32\config\ELAM
C:\Windows\System32\config\Journal
C:\Windows\System32\config\RegBack
C:\Windows\System32\config\systemprofile
C:\Windows\System32\config\TxR
文件管理器向我显示此输出:
C:\Windows\System32\config\BBI
C:\Windows\System32\config\BCD-Template
C:\Windows\System32\config\COMPONENTS
C:\Windows\System32\config\DEFAULT
C:\Windows\System32\config\DRIVERS
C:\Windows\System32\config\ELAM
C:\Windows\System32\config\Journal
C:\Windows\System32\config\netlogon.ftl
C:\Windows\System32\config\RegBack
C:\Windows\System32\config\SAM
C:\Windows\System32\config\SECURITY
C:\Windows\System32\config\SOFTWARE
C:\Windows\System32\config\SYSTEM
C:\Windows\System32\config\systemprofile
C:\Windows\System32\config\TxR
C:\Windows\System32\config\VSMIDK
操作系统:Windows 10
问题是如何使用 C++ 获得相同的输出?
最佳答案
这可能是权限问题,如果您在资源管理器的属性窗口中查看“安全”选项卡,您可能会看到某些文件对“用户”组具有“读取”权限,但某些文件只有权限对于“系统”和“管理员”。
当您在 Windows 中运行程序时,即使是从管理员帐户运行,它通常在没有提升的情况下运行,因此它将无法访问那些具有更受限权限的文件。
您可以显式地以提升的方式运行您的程序,例如右键单击 exe/快捷方式和“以管理员身份运行”。请注意,对于 Visual Studio,您可以以管理员身份运行 VS 本身。
如果您的程序总是需要运行提升,您可以这样设置,在 VS 中,在“链接器”->“ list 文件”上有“UAC 执行级别”选项,“highestAvailable”或“requireAdministrator”选项可能会有用。
如果您正在启动一个子进程,您可以选择在那个点提升,例如使用
ShellExecuteEx
,如果需要,这将导致 UAC 弹出窗口。
关于c++ - 显示 C :\Windows\System32\config using C++ 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59360532/