DeleteSubKey UnauthorizedAccessException [英] DeleteSubKey UnauthorizedAccessException

查看:120
本文介绍了DeleteSubKey UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个快速的应用程序来修改某些注册表项。当我通过注册表编辑器浏览,我可以修改,没有任何问题,删除键。

但是,当我尝试使用 RegistryKey.DeleteSubKey()它抛出一个 UnauthorizedAccessException

有没有什么办法来获得特权做到这一点?另外,为什么会有一个问题,如果我的用户帐号显然访问进行更改?

编辑:

下面是一些code

 的RegistryKey章;

尝试
{
    章= Registry.CurrentUser.OpenSubKey(BaseKey);
    reg.DeleteSubKey({+ Item.Guid.ToString()+});
}
抓住
{
    返回false;
}
 

解决方案

试试这个,而不是最初打开它的读/写,而不是只读:

 的RegistryKey章;

尝试
{
    章= Registry.CurrentUser.OpenSubKey(BaseKey,真正的); //<  - 在这里!
    reg.DeleteSubKey({+ Item.Guid.ToString()+});
}
抓住
{
    返回false;
}
 

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;
}

这篇关于DeleteSubKey UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆