0

How do I add/change OEMInformation registry keys using C#?

I tried to use this code but it returns an error :

 RegistryKey myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\Windows\CurrentVersion\OEMInformation", true);
 myKey.SetValue("manufacturer", "Asus", RegistryValueKind.String);

This error is being returned :

An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll

Additional information: Requested registry access is not allowed.

EDIT :
Also tried this code :

    Microsoft.Win32.RegistryKey key;
    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\Windows\CurrentVersion\OEMInformation");
    key.SetValue("manufacturer", "Asus");
    key.Close();

but this code returns also an error :
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Additional information: Cannot write to the registry key.

And my application already runs as admin using this code :

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  • possible duplicate of [How to force my .NET App to run as administrator on Windows 7?](http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7) – Hans Passant Sep 11 '13 at 13:36
  • You added the code, but is it actually running as administrator? You need to check the project settings that it is actually using the manifest file where you specify the `RequireAdministrator` line. – Scott Chamberlain Sep 11 '13 at 13:42
  • Also if you right click on the Key and go to `Permissions` does it show that [Administrators has full control rights to the Key](http://i.stack.imgur.com/5K0o3.png)? – Scott Chamberlain Sep 11 '13 at 13:53

2 Answers2

1

Visual Studio also has to run in Admin mode.

0

You have to run the program as Administrator to access (modify) LocalMachine key. (eg. add manifest to your program http://msdn.microsoft.com/en-us/library/bb756929.aspx )

Or gain access to every one in this specific registry key ( http://technet.microsoft.com/en-us/library/cc728310(v=ws.10).aspx )

Edit: Ensure your program run with elevated privileges. I created simple Console program and work like a charm.

Additional to your edit: If you open registry key with only one parameter, means you want only read ( http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx ) So you have to use it like before (with two parameters and set second to "true")

Carnifex
  • 533
  • 3
  • 10