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
後者的例子,因爲這是什麼的問題是問? – cja 2014-04-10 14:48:58
我不能相信這是被接受的答案o.O – lewis4u 2017-10-28 12:47:58