private static UInt32 GetHiveValueFromString(RegHives RegistryHive) { //HKEY_CLASSES_ROOT = 2147483648 or 0x80000000 //HKEY_CURRENT_USER = 2147483649 or 0x80000001 //HKEY_LOCAL_MACHINE = 2147483650 or 0x80000002 //HKEY_USERS = 2147483651 or 0x80000003 //HKEY_CURRENT_CONFIG = 2147483653 or 0x80000005 UInt32 TmpRegHive = 0; if (RegistryHive == RegHives.CLASSES_ROOT) { TmpRegHive = 2147483648; } else if (RegistryHive == RegHives.CURRENT_USER) { TmpRegHive = 2147483649; } else if (RegistryHive == RegHives.LOCAL_MACHINE) { TmpRegHive = 2147483650; } else if (RegistryHive == RegHives.USERS) { TmpRegHive = 2147483651; } else if (RegistryHive == RegHives.CURRENT_CONFIG) { TmpRegHive = 2147483653; } return(TmpRegHive); }
private CimMethodParametersCollection SetRegParameters(RegHives RegistryHive, RegistryMethods RegMethod, string SubKeyPath, string AttributeName = "", string Value = "") { string StrRegMethod = Enum.GetName(typeof(RegistryMethods), RegMethod); CimMethodParametersCollection CimParams = GetParametersForMethod(RegistryNameSpace, RegistryClassName, StrRegMethod); CimParams["hDefKey"].Value = GetHiveValueFromString(RegistryHive); CimParams["sSubKeyName"].Value = SubKeyPath; if (StrRegMethod != "CreateKey" && StrRegMethod != "DeletKey" && StrRegMethod != "EnumKey" && StrRegMethod != "EnumValues" && StrRegMethod != "GetSecurityDescriptor") { if (StrRegMethod != "CheckAccess" && StrRegMethod != "SetSecurityDescriptor") { CimParams["sValueName"].Value = AttributeName; if (StrRegMethod == "SetStringValue" || StrRegMethod == "SetMultiStringValue" || StrRegMethod == "SetExpandedStringValue") { CimParams["sValue"].Value = Value; } else if (StrRegMethod == "SetDWORDValue" || StrRegMethod == "SetQWORDValue" || StrRegMethod == "SetBinaryValue") { CimParams["uValue"].Value = Value; } } else if (StrRegMethod == "CheckAccess") { CimParams["uRequired"].Value = Value; } else if (StrRegMethod == "SetSecurityDescriptor") { CimParams["Descriptor"].Value = Value; } } return(CimParams); }
/// <summary> /// Sets the value for a specified String value in the Registry Key for DisplaySwitch or VNC Server /// </summary> /// <param name="reg">Whether the key to be set belongs to Local Machine or Current User</param> /// <param name="valuename">The name of the String to set</param> /// <param name="value">The value to set</param> public static void SetRegistryValue(RegHives reg, string valuename, string value) { RegistryKey regkey; if (reg == RegHives.HKLM) { regkey = RegistryManagement.GetHKLM().CreateSubKey("SOFTWARE\\RealVNC\\vncserver", true); } else if (reg == RegHives.HKLM_DS) { regkey = RegistryManagement.GetHKLM().CreateSubKey("SOFTWARE\\RealVNC_DisplaySwitch", true); } else { regkey = Registry.Users.CreateSubKey(GetSIDForCurrentUser() + "\\SOFTWARE\\RealVNC_DisplaySwitch", true); } regkey.SetValue(valuename, value, RegistryValueKind.String); regkey.Close(); }
/// <summary> /// Retrieves the current value for a specified String value in the Registry Key for DisplaySwitch or VNC Server /// </summary> /// <param name="reg">Whether the key to be set belongs to Local Machine or Current User</param> /// <param name="valuename">The name of the String to retrieve</param> /// <returns>The value of the String</returns> public static string GetRegistryValue(RegHives reg, string valuename) { RegistryKey regkey; if (reg == RegHives.HKLM) { regkey = RegistryManagement.GetHKLM().OpenSubKey("SOFTWARE\\RealVNC\\vncserver", false); } else if (reg == RegHives.HKLM_DS) { regkey = RegistryManagement.GetHKLM().OpenSubKey("SOFTWARE\\RealVNC_DisplaySwitch", false); } else { regkey = Registry.Users.OpenSubKey(GetSIDForCurrentUser() + "\\SOFTWARE\\RealVNC_DisplaySwitch", false); } string value = ""; try { value = regkey.GetValue(valuename).ToString(); regkey.Close(); } catch { } return(value); }