-4

I want to write to this registry

SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run

I tried this:

RegistryKey registryKey64 = RegistryKey
    .OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    .OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run");

registryKey64.SetValue("CTIPopupForIPPhone", Application.ExecutablePath);

and I got this exception:

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

Additional information: Cannot write to the registry key.

though I am running my Visual Studio as an administrator.

Update

During the development, I also tried

RegistryKey registryKey = Registry.CurrentUser
    .OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true);

but I got error that the entry doesn't exist though it is there, but I thought that because the RegistryKey is for Windows 32-bit not 64-bit.

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • can you access this registry through registry editor ? And can u update it from registry editor ? – Kapoor Oct 17 '15 at 15:40
  • @Kapoor and do u normally ask you clients to do that? – Marco Dinatsoli Oct 17 '15 at 15:42
  • 1
    Obviously not !! it was a check if u have the permissions to access the registry at first place ... then one can isolate if its a software issue or a permission (registry issue ) – Kapoor Oct 17 '15 at 15:45
  • 1
    Please use proper capitalization. –  Oct 20 '15 at 09:52
  • Possible duplicate of [cannot write to the registry key](http://stackoverflow.com/questions/7202752/cannot-write-to-the-registry-key) (or possibly [Cannot write to Registry Key, getting UnauthorizedAccessException](http://stackoverflow.com/questions/4463706/cannot-write-to-registry-key-getting-unauthorizedaccessexception)). – Ilmari Karonen Oct 20 '15 at 14:11

1 Answers1

2

This works for me,

RegistryKey registryKey64 = RegistryKey
    .OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    .OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true);

The new variable true is to both read and write over registry entry to allow both of them.

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • It's worth adding a link to [MSDN](https://msdn.microsoft.com/en-us/library/xthy8s8d(v=vs.110).aspx) which explains what the additional Boolean parameter does - "If writable is true, the key will be opened for reading and writing, otherwise, the key will be opened as read-only". – Daniel Kelley Oct 20 '15 at 09:43
  • @DanielKelley exactly, the parameter is for write and read together it – Marco Dinatsoli Oct 20 '15 at 09:44