程序设置开机自启

Windows

// "Key" 是自启名称
// "Value" 程序路径
// 添加注册表
reg add HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v "Key" /d "Value" /f
// 删除注册表
reg delete HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v "Key" /f

WinExec(cmd, SW_HIDE);

MacOS

// 获取二进制程序路径
char * appPath = /Application/Test.app/Contents/MacOS/Test

QStringList exts;
exts << appPath << "--autoRun" << "1";	// 后面带的是启动参数

QSettings plist("~/Library/LaunchAgents/test.plist", QSettings::NativeFormat);
plist.setValue("Label", "test");
plist.setValue("ProgramArguments", exts);
plist.setValue("RunAtLoad", true);

// 关闭开机自启
remove("~/Library/LaunchAgents/test.plist");

Linux

char* pathHome = getenv("HOME");
//std::string profile = std::string(pathHome) + "/.bash_profile";
std::string autostartDir = std::string(pathHome) + "/.config/autostart/";
std::string desktopFile = autostartDir + "raysync-start-on-boot.desktop";
std::string cmd;
if (bEnabled)
{
	cmd = "mkdir -p " + autostartDir;
	system(cmd.c_str());

	cmd = "rm -rf " + desktopFile;
	system(cmd.c_str());
	
	cmd = "echo \\"[Desktop Entry]\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"Name=RaysyncStartOnBoot\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"GenericName=A descriptive name\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"Comment=raysync start on boot\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"Exec=/usr/local/bin/raysync --autoRun 1 &\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"Terminal=false\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"Type=Application\\" >>" + desktopFile;
	system(cmd.c_str());

	cmd = "echo \\"X-GNOME-Autostart-enabled=true\\" >>" + desktopFile;
	system(cmd.c_str());
}
else
{
	cmd = "rm -rf " + desktopFile;
	system(cmd.c_str());
}