紧急求助 Qt添加开机启动项
以下是添加开机启动项的函数,最近发现不能将启动项添加到注册表中去(以前是可以的),总是报错QSettings: failed to set subkey "SPRunSysterm":
各位大大帮忙分析以下是哪里的问题?
void SetRunParamentDlg::on_bBootStart_clicked(bool checked)//开机自动运行
{
QString path = pwnd->sProjectPath;
qDebug() << "path = "<< path;
QString sexetPath = QCoreApplication::applicationDirPath();//当前目录
sexetPath = sexetPath.replace("/","\");
qDebug() << "sexetPath = "<< sexetPath;
if(checked)
{
QFile file("RunSystem.bat");
if (!file.open(QIODevice::Truncate | QIODevice::ReadWrite))
{
QMessageBox::warning(NULL, tr("保存文件"),
tr("无法保存文件 %1:\n %2")
.arg(file.errorString()));
}
QTextStream out(&file);
QString sCmd=""";//sexetPath+"\\SKMonitorSys.exe ";
//sCmd.append(sexetPath+"\\SKMonitorSys.exe");
sCmd.append(""");
sCmd.append(" ");
sCmd.append(""");
sCmd.append(path);
sCmd.append(""");
sCmd.append(" ");
QString sTmp=QString::number(is_tanslator);
sCmd.append(sTmp);
out << sCmd;
qDebug() << "sCmd = "<< sCmd;
file.close();
QDir::setCurrent(sexetPath);
path.clear();
path = sexetPath+"\\RunSystem.bat";
qDebug() << "path = "<< path;
QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
QSettings::NativeFormat);
reg->setValue("RunSysterm",QVariant(path));
delete reg;
}
else
{
//删除RunSystem.bat"
QFile::remove(sexetPath+"\\RunSystem.bat");
//删除注册表
QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
QSettings::NativeFormat);
reg->remove("RunSysterm");
delete reg;
}
}