我想在启动计算机时自动打开应用程序。我从互联网上找到了代码。
我的代码是这样的:
private static void AutoOpen()
{
string starupPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
try
{
run.SetValue("start", starupPath);
loca.Close();
}
catch (Exception ee)
{
throw new Exception("start error :" + ee.ToString());
}
}
当这个程序进入
行时RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
它给我一个这样的例外:
{“拒绝访问注册表项'HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run'。”}
如何避免此异常?