Qt 无法创建/写入 C:\

发布于 2024-10-11 00:14:10 字数 1039 浏览 5 评论 0原文

我正在编写一个 Qt 程序(最初适用于 Windows 7 4.7),需要写入安装目录(C:\Program Files...)。当我尝试写入“受保护”的位置(程序文件、C:\ 等)时,没有创建任何文件。但是,QFile 没有给我任何错误代码(error() 返回 0,这意味着它工作正常)。

这是我正在使用的代码片段,但它不起作用。我正在程序中稍后关闭该文件。

QApplication a(argc, argv);

// Setting plugin paths.
QStringList paths = QCoreApplication::libraryPaths();
paths.append(QCoreApplication::applicationDirPath());
QCoreApplication::setLibraryPaths(paths);



// Debug file.
QString path = QCoreApplication::applicationDirPath() + "/debug.dat";
//QFile debugFile(QCoreApplication::applicationDirPath() + "/debug.dat");
QFile debugFile("C:/debug.txt");
qDebug() << debugFile.error();
debugFile.setPermissions(QFile::WriteUser | QFile::WriteGroup | QFile::WriteOwner | QFile::WriteOther);
debugFile.open(QFile::WriteOnly);
QTextStream debugStream(&debugFile);

// Processing the arguments.
debugStream << QString("Processing Arguments\n");

有人对如何解决这个问题有任何建议吗?

感谢您的帮助,

Jec


添加清单文件是我选择解决此问题的途径。

感谢您的所有帮助。

I am writing a Qt program (4.7 for windows 7 initially) that requires writing to the installed directory (C:\Program Files...). No files are being created when I try to write to a location that would be "protected" (program files, C:\ etc). However, QFile is not giving me any error code (error() is returning 0 which means it worked fine).

Here is a code snippit that I am using that is not working. I am closing the file its just much later in the program.

QApplication a(argc, argv);

// Setting plugin paths.
QStringList paths = QCoreApplication::libraryPaths();
paths.append(QCoreApplication::applicationDirPath());
QCoreApplication::setLibraryPaths(paths);



// Debug file.
QString path = QCoreApplication::applicationDirPath() + "/debug.dat";
//QFile debugFile(QCoreApplication::applicationDirPath() + "/debug.dat");
QFile debugFile("C:/debug.txt");
qDebug() << debugFile.error();
debugFile.setPermissions(QFile::WriteUser | QFile::WriteGroup | QFile::WriteOwner | QFile::WriteOther);
debugFile.open(QFile::WriteOnly);
QTextStream debugStream(&debugFile);

// Processing the arguments.
debugStream << QString("Processing Arguments\n");

Does anyone have any tips on how to solve this problem?

Thanks for the help,

Jec


Adding a manifest file is the route I choose to fix this problem.

Thanks for all of the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萝莉病 2024-10-18 00:14:10

您是否检查过该文件是否未在 VirtualStore 中为该用户创建?检查应用程序和服务日志下的事件查看器 ->微软->窗口 -> UacFile虚拟化->操作。如果您看到事件 ID 为 5000 的条目,则表明发生了 FileCreateVirtualExclude 事件。

检查文件是否未在 %USERPROFILE%\AppData\Local\VirtualStore 下创建。如果确实如此,您可能需要嵌入请求所需权限的清单(即关闭虚拟化)。

有关详细信息,请参阅 适用于 Windows Vista 的新 UAC 技术(向下滚动并查找虚拟化。)

Have you checked whether the file isn't created in the VirtualStore for that user? Check the Event Viewer under Applications and Services Logs -> Microsoft -> Windows -> UacFileVirtualization -> Operational. If you see entries with event ID 5000, a FileCreateVirtualExclude event has occurred.

Check if the file didn't get created under %USERPROFILE%\AppData\Local\VirtualStore. If it did, you might need to embed a manifest requesting the required privileges (i.e., turning virtualization off.)

For more details, see New UAC Technologies for Windows Vista (scroll down and look for Virtualization.)

贪了杯 2024-10-18 00:14:10

您需要获得足够的用户访问权限(即“以管理员身份运行”)才能在 Windows Vista+ 中写入此类文件夹。以管理员身份启动应用程序,或通过调用 WinAPI 请求管理员权限。

You need to acquire sufficient user access rights (ie "Run as Administrator") to write to such folders in Windows Vista+. Either start the app as administrator, or ask for Administrator rights via a call to WinAPI.

冷心人i 2024-10-18 00:14:10

QFile 可能会向您提供错误代码,但您未能检查它。

您应该做的事情更像是:

if (!debugFile.open(QFile::WriteOnly)) {
    qWarning() << "Failed to open" << debugFile.fileName() << "for write:" << debugFile.errorString();
}

您已经检查了 QFile::error 的返回值,但仅在调用 open 之前 - 您需要打开尝试后检查

QFile may be giving you an error code, but you've failed to check for it.

You should do something more like:

if (!debugFile.open(QFile::WriteOnly)) {
    qWarning() << "Failed to open" << debugFile.fileName() << "for write:" << debugFile.errorString();
}

You've checked the return value of QFile::error, but only before calling open - you need to check after the open attempt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文