简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM