C#注册表SetValue引发UnauthorizedAccessException [英] C# Registry SetValue throws UnauthorizedAccessException

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

问题描述

在尝试通过进行快速Google搜索"回答此问题之前.我想指出我已经.在这种情况下,我有以下尝试修改注册表项值的方法.我遇到的问题是,即使我已将键打开为可写状态,执行该命令时也会抛出UnauthorizedAccessException .我正在以管理员身份运行Visual Studio,甚至试图用清单文件制作一个小的.exe,迫使它以admin身份运行,这将使代码执行不顺利.密钥已经存在,它不会尝试进入CreateKey方法.这是代码块.

Before you try to answer this with, "Do a quick Google search." I'd like to point out that I have already. Here is the situation, I have the following method that attempts to modify a registry key value. The problem I'm getting is that when executed, it throws an UnauthorizedAccessException even though I've opened the key as writeable. I'm running Visual Studio as administrator and even tried to make a small .exe with a manifest file forcing it to run as admin that will execute the code with no luck. The key already exists, it doesn't try to go into the CreateKey method. Here is the block of code.

Path = "S-1-5-21-1644491937-1078145449-682003330-5490\Software\Microsoft\Windows\CurrentVersion\Policies\System"
Key = "DisableTaskMgr"
NewValue = 1

public OperationResult ModifyKey()
    {
        OperationResult result = new OperationResult();

        if (!Path.IsNullOrEmptyTrim())
        {
            if (!Key.IsNullOrEmptyTrim())
            {
                try
                {
                    var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, true);

                    if (key != null)
                    {
                        key.SetValue(Key, NewValue);

                        key.Close();
                    }
                    else
                    {
                        result = CreateKey();
                    }
                }
                catch (Exception ex)
                {
                    result.SetFail("Error accessing registry", ex);
                }
            }
            else
            {
                result.SetFail("Registry key was null");
            }
        }
        else
        {
            result.SetFail("Registry path was null");
        }

        return result;
    }

我是否必须手动遍历注册表树,将每个OpenSubKey调用设置为可写?我也尝试过,但仍然抛出相同的错误...

Do I have to manually walk down the registry tree setting each OpenSubKey call to writeable? I tried this as well, still threw the same error...

推荐答案

为弄清到底发生了什么,我创建了一个简单的服务来测试将作为本地系统帐户运行的代码.这是我尝试运行代码所能想到的最高特权.使用这些权限运行代码即可.

As a last ditch effort to figure out what was going on, I created a simple service to test this code that will run as the local system account. It's the highest privileges I could think of to try and run the code with. Running the code with these permissions worked.

特别感谢 0_ _ ___ 0 Charleh 也指出了防病毒软件.我检查了日志,结果发现它试图隔离我的更改.我想即使这样也不会阻止System用户进行这些更改.

Special thanks go out to 0_____0 and Charleh for pointing out the anti-virus as well. I checked the logs and it turns out it was trying to quarantine my changes. I guess even it won't stop the System user from making these changes though.

特别感谢 Sorceri ,帮助我进行了很多研究.

Special thanks go out to Sorceri as well for helping me research this so much.

总而言之,如果您的行为是间歇性的,非常奇怪的,请检查病毒扫描程序和权限.

In conclusion, if you're having intermittent, extremely odd behavior, check your virus scanner and permissions.

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

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