我正在编写一个从注册表加载参数的应用程序。下面是我用来加载它的代码:
public bool getRegValues() //get values used for the SQL Connection etc
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARECompanyApplicationNightJob", 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."
当我使用注册表编译代码时。当前用户而不是注册表。LocalMachine返回true(当然不同位置的值是相同的)。
怎么了?我是域管理员,也给了自己明确的完全控制权限密钥HKEY_LOCAL_MACHINESOFTWARECompanyApplicationNightJob
任何想法?
如果您使用的是。net 4,请使用:
using(RegistryKey SoftwareKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(@"SOFTWARECompanyApplicationNightJob", RegistryKeyPermissionCheck.ReadWriteSubTree))