1.Regedit自启动注册表路径
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
2.获取运行程序路径
SetAutoStart(AppDomain.CurrentDomain.FriendlyName, AppDomain.CurrentDomain.BaseDirectory);
3.添加到注册表中,如果注册表已经存在此程序,则修改路径
private void SetAutoStart(string appName, string appPath)
{
try
{
RegistryKey runs = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (runs == null)
{
LOG.Warn("registry key empty => Key:SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run}");
return;
}
string path = appPath + appName;
if (runs.GetValue(appName) != null)
{
if (runs.GetValue(appName).ToString() != path)
{
runs.DeleteValue(appName);
LOG.InfoFormat("Successfully updated registry path. \nAppName:{0} Path:{1}", appName, path);
}
}
runs.SetValue(appName, path);
runs.Close();
}
catch (Exception ex)
{
LOG.Warn("registry key fail => ", ex);
}
}