因为开机启动时的当前工作路径为 system32文件
配置文件最好使用绝对路径保存
QString strIniPath = QCoreApplication::applicationDirPath() + "/config.ini";
qDebug()<<"ini file:"<<strIniPath;
if(read_file.LoadFile(strIniPath.toStdString().c_str()))
//
/// \brief Widget::appAutoRun
/// \param bAutoRun
///
/// window下开机启动最简单的实现方式就是在注册表中添加启动项目 添加位置有两个
/// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
/// HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
/// 第一个为全局自动启动项,所有用户登陆后都自动启动。第二个为当前用户启动项、切换到其他用户就失效了
/// win7以后权限管理越来越严格,非管理员权限运行的程序是无法写入到HKEY_LOCAL_MACHINE下的,推荐添加启动项到当前用户
#include <QSettings>
#include <QDir>
#include <QDebug>
void Widget::appAutoRun(bool bAutoRun)
{
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
if (bAutoRun)
{
QString strAppPath=QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
qDebug()<<"path==="<<strAppPath;
//strAppPath.replace(QChar('/'),QChar('\\'),Qt::CaseInsensitive);
reg.setValue("wirtepad",strAppPath);
}
else
{
reg.setValue("wirtepad","");
}
}