private static bool SetAutoRun(string keyName, string filePath)
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.SetValue(keyName, filePath);
runKey.Close();
}
catch
{
return false;
}
return true;
}
private static bool DeleteAutoRun(string keyName)
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.DeleteValue(keyName);
runKey.Close();
}
catch
{
return false;
}
return true;
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.SetValue(keyName, filePath);
runKey.Close();
}
catch
{
return false;
}
return true;
}
private static bool DeleteAutoRun(string keyName)
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.DeleteValue(keyName);
runKey.Close();
}
catch
{
return false;
}
return true;
}
调用:
IsAutoRun = AppConfig.GetAutoRun();
if (IsAutoRun)
{
SetAutoRun("应用程序名称", Application.ExecutablePath);
}
else
{
DeleteAutoRun("应用程序名称");
}