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

C# Unable to load registry values in LocalMachine

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

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

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

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

怎么了?我是域管理员,并且还明确授予了自己对密钥 HKEY_LOCAL_MACHINE\SOFTWARE\Company\Application\NightJob\

的完全控制权限

有什么想法吗?

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

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