C#securityexception does not allow the requested registry access

Source: Internet
Author: User

"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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.