C#无法在LocalMachine中加载注册表值

时间:2016-11-08 17:33:57

标签: c# sql permissions registry

我正在编写一个从注册表加载参数的应用程序。这是我用来加载它的代码:

    public bool getRegValues() //get values used for the SQL Connection etc
    {
        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Company\Application\NightJob\", RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            if (key != null)
            {
                this.serverName = key.GetValue("SQLserver").ToString();
                this.timeout = key.GetValue("timeout").ToString();
                this.Database = key.GetValue("database").ToString();
                this.logTable = key.GetValue("table_log").ToString();
                this.budgetTable = key.GetValue("table_budget").ToString();
                this.personsTable = key.GetValue("table_persons").ToString();
                this.tempTable = key.GetValue("table_temp").ToString();
                this.cashiersDB = key.GetValue("cashiersDB").ToString();
                this.customersTbl = key.GetValue("cashiersCustomersTable").ToString();

                key.SetValue("version", version);
                if (this.serverName == null || this.timeout == null || this.Database == null || this.logTable == null
                    || this.budgetTable == null || this.personsTable == null || this.tempTable == null)
                {
                    Console.WriteLine("One of the values could not be loaded.");
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Key is null.");
                return false;
            }
            return true;
        }
    }

当我在工作站上运行代码时,一切都很完美。当我在服务器上执行它时,它返回false并写入“Key is null。”。

当我使用Registry.CurrentUser而不是Registry.LocalMachine编译代码时,它返回true(当然,不同位置的值相同)。

有什么问题?我是域管理员,并且还明确给予我自己对密钥HKEY_LOCAL_MACHINE \ SOFTWARE \ Company \ Application \ NightJob \

的完全控制权限

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您使用.Net 4,请尝试使用:

using(RegistryKey SoftwareKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\Company\Application\NightJob\", RegistryKeyPermissionCheck.ReadWriteSubTree))