"Reprint"C#securityexception does not allow the requested registry access
The boot from startup program is as follows:
if (! System.IO.File.Exists (filename)) throw new Exception ("The file does not exist!") "); String name = filename. Substring (filename. LastIndexOf ("\ \") + 1); Reg = Registry.localmachine.OpenSubKey (@ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if (reg = = null) { reg = Registry.LocalMachine.CreateSubKey (@ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); } if (isautorun) Reg. SetValue (name, filename); Else Reg. SetValue (name, false);
Then the exception pops up:SecurityException does not allow the requested registry access.
Looking for a long time to say what authority is not enough, not the answer I need. Here's how to fix it:
1, right-click the generated EXE application, run as administrator, if no problem, it is possible to access to modify the registry;
However, this is not possible, because after you reboot, you will be able to eject the exception directly, saying that the requested registry access is not allowed. The problem is still unresolved.
2, generally under Win7, vs Access Registry HKEY_LOCAL_MACHINE, that is, the LocalMachine in the program needs to run as an administrator. But it's no problem to visit HKEY_CURRENT_USER.
So modify the program as follows:
...
reg = registry.currentuser. OpenSubKey (@ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if (reg = = null) {reg = Registry.CurrentUser.CreateSubKey (@ "Software\microsoft\window S\currentversion\run "); }
...
OK, yes.
Workaround two: That is, log in as Administrator, or join the current user to the Administrators group
C#securityexception does not allow the requested registry access