简体   繁体   中英

DeleteSubKey UnauthorizedAccessException

I'm trying to write a quick app to modify some registry keys. When I'm browsing via RegEdit, I can modify and delete the keys with no problems.

But when I try to use RegistryKey.DeleteSubKey() it throws an UnauthorizedAccessException .

Is there any way to gain the privileges to do this? Also, why would there be a problem if my user account obviously has access to make the changes?

Edit:

Here's some code

RegistryKey reg;

try
{
    reg = Registry.CurrentUser.OpenSubKey(BaseKey);
    reg.DeleteSubKey("{" + Item.Guid.ToString() + "}");
}
catch
{
    return false;
}

Try this instead, open it initially as read/write instead of read-only:

RegistryKey reg;

try
{
    reg = Registry.CurrentUser.OpenSubKey(BaseKey, true); //<--over here!
    reg.DeleteSubKey("{" + Item.Guid.ToString() + "}");
}
catch
{
    return false;
}

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