繁体   English   中英

C#将字符串添加到注册表

[英]C# Add a String to the Registry

我已经搜索了一段时间,但没有找到任何东西。

它有效,但不完全有效。 它在注册表中找到文件夹,但不会创建字符串。 我没有任何关于权限之类的错误。 我以管理员身份运行。

            try
        {
            RegistryKey registryKey = Registry.LocalMachine;
            registryKey.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\OEMInformation", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.None);

            registryKey.SetValue("Test1","Test2");
            registryKey.Close();

            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
            return false;
        }

我究竟做错了什么? 还是我错过了重要的事情?

感谢DaNeubi

感谢@ stephen.vakil,我得以解决它。

现在是我的代码:

       public static Boolean WriteToRegistry(String[] OEMInformations)
    {
        try
        {
            for (int i = 0; i < OEMInformations.Length; i = i +2)
            {
                RegistryKey mainKey = Registry.LocalMachine;
                RegistryKey firstKey = mainKey.OpenSubKey("SOFTWARE", true);
                RegistryKey secondKey = firstKey.OpenSubKey("Microsoft", true);
                RegistryKey thirdKey = secondKey.OpenSubKey("Windows", true);
                RegistryKey fourthKey = thirdKey.OpenSubKey("CurrentVersion", true);
                RegistryKey fifthKey = fourthKey.OpenSubKey("OEMInformation", true);

                fifthKey.SetValue(OEMInformations[i],OEMInformations[i + 1], RegistryValueKind.String);

                fifthKey.Close();
                fourthKey.Close();
                thirdKey.Close();
                secondKey.Close();
                firstKey.Close();
                mainKey.Close();
            }
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
            return false;
        }
    }

暂无
暂无

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

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