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.
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.)
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.
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.
来源:https://stackoverflow.com/questions/4640730/qt-cannot-cannot-create-write-to-c