问题: Qt5.9.4再注册表中加入开机自启动后,每次开机可以启动,但是无法读取配置文件
解决方案 再main()方法中加入QDir::setCurrent(QCoreApplication::applicationDirPath());
即可。
static void AutoRunWithSystem(bool bAutoRun)
{// 获取当前程序路径QString appPath = QCoreApplication::applicationDirPath()+"/WatchDog.exe";// 将当前程序添加到开机启动项QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
// appPath = appPath + " /autorun";appPath=appPath.replace("/","\\");settings.setValue("MyWatchDog", appPath);qDebug()<<"auto start:"<<appPath;
}int main(int argc, char *argv[])
{QApplication a(argc, argv);QDir::setCurrent(QCoreApplication::applicationDirPath());qInstallMessageHandler(Logger::CustomMessageHandler);// 设置应用程序图标QIcon icon(":/dog.ico");QApplication::setWindowIcon(icon);AutoRunWithSystem(true);qDebug()<<"Auto Restart!";MainWindow win;win.setWindowIcon(icon);win.show();return a.exec();
}