简体   繁体   English

C# 注册表 SetValue 抛出 UnauthorizedAccessException

[英]C# Registry SetValue throws UnauthorizedAccessException

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 .我遇到的问题是,在执行时,即使我已将密钥打开为可写,它也会抛出 UnauthorizedAccessException 。 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.我正在以管理员身份运行 Visual Studio,甚至试图制作一个带有清单文件的小型 .exe,迫使它以管理员身份运行,这将在没有运气的情况下执行代码。 The key already exists, it doesn't try to go into the CreateKey method.密钥已经存在,它不会尝试进入 CreateKey 方法。 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;
    }

Do I have to manually walk down the registry tree setting each OpenSubKey call to writeable?我是否必须手动遍历注册表树,将每个 OpenSubKey 调用设置为可写? I tried this as well, still threw the same error...我也试过这个,仍然抛出同样的错误......

in the var for your key在你的钥匙的 var 中

var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, true);

change to改成

var key = Microsoft.Win32.Registry.Users.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree);  

Have you tried setting the accessrule and permissions?您是否尝试过设置访问规则和权限?

 string user = Environment.UserDomainName + "\\" + Environment.UserName;
 RegistryAccessRule rule = new RegistryAccessRule(user,
        RegistryRights.FullControl,
        AccessControlType.Allow);        
 RegistrySecurity security = new RegistrySecurity();
 security.AddAccessRule(rule);
 var key = Microsoft.Win32.Registry.Users.OpenSubKey(subKeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
 key.SetAccessControl(security);

One possible issue that I see with your code is that the Path variable is being set to a string that doesn't escape the \\ characters.我在您的代码中看到的一个可能问题是Path变量被设置为一个不会转义\\字符的字符串。 How about something like:怎么样:

Path = @"S-1-5-21-1644491937-1078145449-682003330-5490\Software\Microsoft\Windows\CurrentVersion\Policies\System";

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.使用这些权限运行代码有效。

Special thanks go out to 0_ _ ___0 and Charleh for pointing out the anti-virus as well.特别感谢0_ _ ___0Charleh 也指出了防病毒软件。 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.我想即使它也不会阻止系统用户进行这些更改。

Special thanks go out to Sorceri as well for helping me research this so much.特别感谢Sorceri帮助我研究了这么多。

In conclusion, if you're having intermittent, extremely odd behavior, check your virus scanner and permissions.总之,如果您有间歇性的、极其奇怪的行为,请检查您的病毒扫描程序和权限。

I ran into the same problem recently.我最近遇到了同样的问题。 So I tried a few things and instead of calling key.SetValue(Key, NewValue) simply calling create function solved my problem.所以我尝试了一些事情,而不是调用key.SetValue(Key, NewValue)简单地调用 create 函数解决了我的问题。 That is;那是;

Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.Users.CreateSubKey(Path);
key1.SetValue(Key, NewValue);

CreateSubKey call doesn't delete the current entry but provided with the ability to write without exception. CreateSubKey 调用不会删除当前条目,但提供了无一例外的写入能力。 I hope that helps.我希望这有帮助。

Only set grants to dword.仅将授权设置为 dword。 You must to open Registry and at the last folder path, rigth click over it and set Grants, and select All Aplications and check Total Control.您必须打开注册表并在最后一个文件夹路径,右键单击它并设置授予,然后选择所有应用程序并检查总控制。 I hope to help you.我希望能帮助你。

just Registry.SetValue(sub_key, key, value);只是Registry.SetValue(sub_key, key, value);

Example:例子:

Registry.SetValue(
            @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
            "MyApp",
            Application.ExecutablePath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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