Windows-Autostart: Application directory seems to be C:/Windows/system32/

南笙酒味 提交于 2019-12-10 23:56:58

问题


When my app is started from the autostart of Windows, Qt is not able to open it. The error that causes this is that QDir().absoluteFilePath("settings.cnf") returns the path C:/Windows/system32/settings.cnf even though my file is located in my working directory.

Does someone know what method I must call to get the right path?


回答1:


Using the default constructor QDir() defaults to the current working directory, which can be anything, depending from where the user or the system started the application. For a UI application, that's a path one should usually ignore completely.

To access data next to your application binary, use QCoreApplication::applicationDirPath(). This is usually used for global read-only data installed with the program.

For user-writable configuration settings and cached data, use QStandardPaths (Qt 5) or QDesktopServices::storageLocation() (Qt 4).




回答2:


There is no particular reason for the working directory of a GUI program to be defined. Since relative paths are relative to the working directory, the use of relative paths impies a well defined and meaningful working directory.

Now, you actually want to look for a file in the same directory as the executable. So the correct approach is to find the full path to the executable, strip off the file name, and append your file name. Now you have the full path to the file.

As a general rule, the working directory is only meaningful for console applications. So only use relative paths in a console application.



来源:https://stackoverflow.com/questions/21075737/windows-autostart-application-directory-seems-to-be-c-windows-system32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!