반응형
C #을 사용하여 레지스트리 값이 있는지 확인하는 방법은 무엇입니까?
C # 코드로 레지스트리 값이 있는지 확인하는 방법은 무엇입니까? 이것은 내 코드입니다. '시작'이 있는지 확인하고 싶습니다.
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);
}
레지스트리 키의 경우 가져온 후 null인지 확인할 수 있습니다. 존재하지 않는다면 그럴 것입니다.
레지스트리 값의 경우 현재 키의 값 이름을 가져와이 배열에 필요한 값 이름이 포함되어 있는지 확인할 수 있습니다.
예:
public static bool checkMachineType()
{
RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);
return (winLogonKey.GetValueNames().Contains("Start"));
}
public static bool RegistryValueExists(string hive_HKLM_or_HKCU, string registryRoot, string valueName)
{
RegistryKey root;
switch (hive_HKLM_or_HKCU.ToUpper())
{
case "HKLM":
root = Registry.LocalMachine.OpenSubKey(registryRoot, false);
break;
case "HKCU":
root = Registry.CurrentUser.OpenSubKey(registryRoot, false);
break;
default:
throw new System.InvalidOperationException("parameter registryRoot must be either \"HKLM\" or \"HKCU\"");
}
return root.GetValue(valueName) != null;
}
string keyName=@"HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\pcmcia";
string valueName="Start";
if (Registry.GetValue(keyName, valueName, null) == null)
{
//code if key Not Exist
}
else
{
//code if key Exist
}
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
}
public bool ValueExists(RegistryKey Key, string Value)
{
try
{
return Key.GetValue(Value) != null;
}
catch
{
return false;
}
}
이 간단한 함수는 값이 발견되었지만 null이 아닌 경우에만 true를 반환하고, 그렇지 않으면 값이 있지만 null이거나 값이 키에없는 경우 false를 반환합니다.
질문에 대한 사용법 :
if (ValueExists(winLogonKey, "Start")
{
// The values exists
}
else
{
// The values does not exists
}
RegistryKey test9999 = Registry.CurrentUser;
foreach (var item in test9999.GetSubKeyNames())
{
if (item.ToString() == "SOFTWARE")
{
test9999.OpenSubKey(item);
foreach (var val in test9999.OpenSubKey(item).GetSubKeyNames())
{
if(val.ToString() == "Adobe") {
Console.WriteLine(val+ " found it ");
}
}
}
}
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
참고 URL : https://stackoverflow.com/questions/4276138/how-to-check-if-a-registry-value-exists-using-c
반응형
'IT TIP' 카테고리의 다른 글
| AppStore-앱 상태가 판매 준비가되었지만 앱 스토어에 없습니다. (0) | 2020.10.29 |
|---|---|
| XSLT를 사용하여 XML에서 CSV로 (0) | 2020.10.29 |
| git 상태가 준비된 파일 만 표시하도록하는 방법 (0) | 2020.10.29 |
| GSON은 왜 getter / setter가 아닌 필드를 사용합니까? (0) | 2020.10.29 |
| data.table 열에서 텍스트 문자열 분할 (0) | 2020.10.29 |