I want my application auto open while I start my computer. I found the code from Internet.
My code is like this:
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());
}
}
While this program goes to the line
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
It gives me an exception like this:
{"Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' is denied."}
How do I avoid this exception?