网上找的一段代码,但会报错
void RegRun(string appName, bool f)
{
RegistryKey HKCU = Registry.CurrentUser;
RegistryKey Run = HKCU.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
bool b = false;
foreach (string i in Run.GetValueNames())
{
if (i == appName)
{
b = true;
break;
}
}
try
{
if (f)
{
Run.SetValue(appName, Application.ExecutablePath);
}
else
{
Run.DeleteValue(appName);
}
}
catch
{ }
HKCU.Close();
}
}