Tom
Tom

Reputation:

How do I set a registry value in Windows Vista using C#?

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?

Upvotes: 0

Views: 2644

Answers (2)

JaredPar
JaredPar

Reputation: 754605

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.

Upvotes: 1

Preet Sangha
Preet Sangha

Reputation: 65496

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.

Upvotes: 1

Related Questions