如何使用C#检查注册表值是否存在?
如何通过C代码检查注册表值是否存在? 这是我的代码,我想检查“开始”是否存在如何使用C#检查注册表值是否存在?,c#,registry,C#,Registry,如何通过C代码检查注册表值是否存在? 这是我的代码,我想检查“开始”是否存在 public static bool checkMachineType() { RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true); string currentKey= winLogonKey.GetValue("Start").ToS
public static bool checkMachineType()
{
RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);
string currentKey= winLogonKey.GetValue("Start").ToString();
if (currentKey == "0")
return (false);
return (true);
}
对于注册表项,您可以在获取注册表项后检查它是否为空。如果它不存在,它就会存在 对于注册表值,可以获取当前项的值名称,并检查此数组是否包含所需的值名称 例如:
public static bool checkMachineType()
{
RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);
return (winLogonKey.GetValueNames().Contains("Start"));
}
此简单函数仅在找到值但该值不为null时返回true,如果该值存在但为null或该值在键中不存在,则将返回false
问题的用法:
if (ValueExists(winLogonKey, "Start")
{
// The values exists
}
else
{
// The values does not exists
}
当然,“法格纳·安图内斯·多内尔斯”的回答是正确的。但在我看来,另外还值得检查注册表分支本身,或者确保该部分确实存在
例如(“脏黑客”),我需要在RMS基础设施中建立信任,否则当我打开Word或Excel文档时,将提示我输入“Active Directory权限管理服务”。下面是我如何在企业基础架构中为我的服务器添加远程信任的方法
foreach (var strServer in listServer)
{
try
{
RegistryKey regCurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC\\{strServer}", false);
if (regCurrentUser == null)
throw new ApplicationException("Not found registry SubKey ...");
if (regCurrentUser.GetValueNames().Contains("UserConsent") == false)
throw new ApplicationException("Not found value in SubKey ...");
}
catch (ApplicationException appEx)
{
Console.WriteLine(appEx);
try
{
RegistryKey regCurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC", true);
RegistryKey newKey = regCurrentUser.CreateSubKey(strServer, true);
newKey.SetValue("UserConsent", 1, RegistryValueKind.DWord);
}
catch(Exception ex)
{
Console.WriteLine($"{ex} Pipec kakoito ...");
}
}
}
@hsanders即使问题已经回答了,也欢迎添加有用的信息。堆栈溢出接收来自Google的大量流量;)root.GetValue(valueName)!=如果valueName不存在,null将引发异常。是否应更改为返回根?.GetValue(valueName)!=无效的如果valueName不存在,GetValue会引发哪个异常?后者的例子,因为这就是问题的问题所在?我不相信这是公认的答案。其他解决方案是检查键是否存在。对于value,我们必须检查该键中的值列表
GetValue
的返回类型永远不会是RegistryKey
类型,那么为什么要强制转换它呢?
RegistryKey rkSubKey = Registry.CurrentUser.OpenSubKey(" Your Registry Key Location", false);
if (rkSubKey == null)
{
// It doesn't exist
}
else
{
// It exists and do something if you want to
}
internal static Func<string, string, bool> regKey = delegate (string KeyLocation, string Value)
{
// get registry key with Microsoft.Win32.Registrys
RegistryKey rk = (RegistryKey)Registry.GetValue(KeyLocation, Value, null); // KeyLocation and Value variables from method, null object because no default value is present. Must be casted to RegistryKey because method returns object.
if ((rk) == null) // if the RegistryKey is null which means it does not exist
{
// the key does not exist
return false; // return false because it does not exist
}
// the registry key does exist
return true; // return true because it does exist
};
// usage:
/* Create Key - while (loading)
{
RegistryKey k;
k = Registry.CurrentUser.CreateSubKey("stuff");
k.SetValue("value", "value");
Thread.Sleep(int.MaxValue);
}; // no need to k.close because exiting control */
if (regKey(@"HKEY_CURRENT_USER\stuff ... ", "value"))
{
// key exists
return;
}
// key does not exist
public bool ValueExists(RegistryKey Key, string Value)
{
try
{
return Key.GetValue(Value) != null;
}
catch
{
return false;
}
}
if (ValueExists(winLogonKey, "Start")
{
// The values exists
}
else
{
// The values does not exists
}
foreach (var strServer in listServer)
{
try
{
RegistryKey regCurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC\\{strServer}", false);
if (regCurrentUser == null)
throw new ApplicationException("Not found registry SubKey ...");
if (regCurrentUser.GetValueNames().Contains("UserConsent") == false)
throw new ApplicationException("Not found value in SubKey ...");
}
catch (ApplicationException appEx)
{
Console.WriteLine(appEx);
try
{
RegistryKey regCurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC", true);
RegistryKey newKey = regCurrentUser.CreateSubKey(strServer, true);
newKey.SetValue("UserConsent", 1, RegistryValueKind.DWord);
}
catch(Exception ex)
{
Console.WriteLine($"{ex} Pipec kakoito ...");
}
}
}