views:

1262

answers:

2
try
{
    RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(
         "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    if (rkApp.GetValue("AdobeBitmapViewer") == null)
    {
        rkApp.SetValue("AdobeBitmapViewer", Application.ExecutablePath.ToString());
    }
    rkApp.Close();
}
catch (Exception) { }

This code works in Windows XP, but in Windows Vista I get an UnauthorizedException. Is there any way to bypass the UAC in Vista to set a registry key?

+1  A: 

I've seen pages saying use CreateKey as opposed to OpenKey - does that make a difference?

You may need to run as elevated authority. This may example help.

See here for another example of seeting rights.

Preet Sangha
Elevation is needed for Registry.CurrentUser.
Michael
I mean, not needed for Registry.CurrentUser.
Michael
ok check the exception and see what's missing.
Preet Sangha
+1  A: 

This should not be a UAC issue. The key in question is in HKCU which is typically not protected by UAC. UAC typically removes your access to the keys like HKLM.

It is possible that a program on Vista came by and created that key with Admin priviledges and baring you from writing to the key on normal circumstances. Can you try passing false (means read only) and see if that allows you to open it? If so you should look at the actual permissions on the key and see what they are.

JaredPar
I can confirm that the Tom's code works on my vanilla Vista machine.
brianpeiris
note: the AdobeBitmapViewer key did not exist on my machine, so the key was created when I ran the code.
brianpeiris