示例#1
0
 public static bool SetRegistryValue(eRegistryRoot rootType, string keyPath, string paramterName,
                                     object value, RegistryValueKind valueType)
 {
     try
     {
         //Get the registry key
         RegistryKey regKey = GetRegistryKey(rootType, keyPath, true);
         if (regKey != null)
         {
             //set the value
             regKey.SetValue(paramterName, value, valueType);
             return(true);
         }
         else
         {
             //Failed to get the registry key
             return(false);
         }
     }
     catch (Exception ex)
     {
         Reporter.ToLog(eLogLevel.ERROR, "Failed to set the registry key value: '" + value +
                        "' for the RootType: '" + rootType + "', KeyPath: '" + keyPath + "' and ParameterName: '" + paramterName + "'", ex);
         return(false);
     }
 }
示例#2
0
 public static object GetRegistryValue(eRegistryRoot rootType, string keyPath, string paramterName)
 {
     try
     {
         //Get the registry key
         RegistryKey regKey = GetRegistryKey(rootType, keyPath);
         if (regKey != null)
         {
             //check if required parameter exist
             if ((regKey.GetValueNames()).Contains(paramterName))
             {
                 //return parameter value
                 return(regKey.GetValue(paramterName));
             }
             else
             {
                 //required parameter was not found
                 Reporter.ToLog(eLogLevel.WARN, "Failed to get the registry key value for the RootType: '" + rootType +
                                "', KeyPath: '" + keyPath + "' and ParameterName: '" + paramterName + "'- Parameter was not found");
                 return(null);
             }
         }
         else
         {
             //Failed to get the registry key
             return(null);
         }
     }
     catch (Exception ex)
     {
         Reporter.ToLog(eLogLevel.ERROR, "Failed to get the registry key value for the RootType: '" + rootType +
                        "', KeyPath: '" + keyPath + "' and ParameterName: '" + paramterName + "'", ex);
         return(null);
     }
 }
示例#3
0
        public static bool CreateRegistryKey(eRegistryRoot rootType, string keyPath)
        {
            RegistryKey regKey = null;

            try
            {
                if (GetRegistryKey(rootType, keyPath) == null)
                {
                    switch (rootType)
                    {
                    case eRegistryRoot.HKEY_CLASSES_ROOT:
                        regKey = Registry.ClassesRoot.CreateSubKey(keyPath);
                        break;

                    case eRegistryRoot.HKEY_CURRENT_USER:
                        regKey = Registry.CurrentUser.CreateSubKey(keyPath);
                        break;

                    case eRegistryRoot.HKEY_LOCAL_MACHINE:
                        regKey = Registry.LocalMachine.CreateSubKey(keyPath);
                        break;

                    case eRegistryRoot.HKEY_USERS:
                        regKey = Registry.Users.CreateSubKey(keyPath);
                        break;

                    case eRegistryRoot.HKEY_CURRENT_CONFIG:
                        regKey = Registry.CurrentConfig.CreateSubKey(keyPath);
                        break;
                    }

                    if (regKey != null)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    //registry key already exist
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Reporter.ToLog(eLogLevel.ERROR, "Failed to create the registry key for the RootType: '" + rootType +
                               "' and KeyPath: '" + keyPath + "'", ex);
                return(false);
            }
        }
示例#4
0
        public static RegistryKey GetRegistryKey(eRegistryRoot rootType, string keyPath, bool writeAcceessNeeded = false)
        {
            RegistryKey regKey = null;

            try
            {
                //return the registry key
                switch (rootType)
                {
                case eRegistryRoot.HKEY_CLASSES_ROOT:
                    regKey = Registry.ClassesRoot.OpenSubKey(keyPath, writeAcceessNeeded);
                    break;

                case eRegistryRoot.HKEY_CURRENT_USER:
                    regKey = Registry.CurrentUser.OpenSubKey(keyPath, writeAcceessNeeded);
                    break;

                case eRegistryRoot.HKEY_LOCAL_MACHINE:
                    regKey = Registry.LocalMachine.OpenSubKey(keyPath, writeAcceessNeeded);
                    break;

                case eRegistryRoot.HKEY_USERS:
                    regKey = Registry.Users.OpenSubKey(keyPath, writeAcceessNeeded);
                    break;

                case eRegistryRoot.HKEY_CURRENT_CONFIG:
                    regKey = Registry.CurrentConfig.OpenSubKey(keyPath, writeAcceessNeeded);
                    break;
                }
                return(regKey);
            }
            catch (Exception ex)
            {
                Reporter.ToLog(eLogLevel.ERROR, "Failed to get the registry key for the RootType: '" + rootType +
                               "' and KeyPath: '" + keyPath + "'", ex);
                regKey = null;
                return(regKey);
            }
        }
示例#5
0
        public static bool CheckRegistryValueExist(eRegistryRoot rootType, string keyPath, string paramterName,
                                                   object expectedValue, Microsoft.Win32.RegistryValueKind expectedValueType,
                                                   bool addValueIfMissing, bool silentMode)
        {
            bool addValue = false;

            try
            {
                if ((GetRegistryValue(rootType, keyPath, paramterName) != null) &&
                    GetRegistryValue(rootType, keyPath, paramterName).ToString() == expectedValue.ToString())
                {
                    return(true);
                }
                else
                {
                    if (addValueIfMissing)
                    {
                        if (silentMode)
                        {
                            addValue = true;
                        }
                        else
                        {
                            //ask user
                            if ((Reporter.ToUser(eUserMsgKey.AddRegistryValue, rootType + "\\" + keyPath)) == eUserMsgSelection.Yes)
                            {
                                addValue = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }

                    if (addValue)
                    {
                        //create the registry key if needed
                        if (CreateRegistryKey(rootType, keyPath))
                        {
                            //add the registry value
                            if (SetRegistryValue(rootType, keyPath, paramterName, expectedValue, expectedValueType))
                            {
                                //succeed to add the value
                                if (!silentMode)
                                {
                                    //show success to user
                                    Reporter.ToUser(eUserMsgKey.AddRegistryValueSucceed, rootType + "\\" + keyPath);
                                }
                                return(true);
                            }
                            else
                            {
                                //failed to add the value
                                if (!silentMode)
                                {
                                    //show failure to user
                                    Reporter.ToUser(eUserMsgKey.AddRegistryValueFailed, rootType + "\\" + keyPath);
                                }
                                return(false);
                            }
                        }
                        else
                        {
                            if (!silentMode)
                            {
                                //show failure to user
                                Reporter.ToUser(eUserMsgKey.AddRegistryValueFailed, rootType + "\\" + keyPath);
                            }
                            return(false);
                        }
                    }
                }
                return(false);
            }
            catch (Exception ex)
            {
                Reporter.ToLog(eLogLevel.ERROR, "Failed to complete the registry value check for the key: " +
                               rootType + "\\" + keyPath, ex);
                if (!silentMode)
                {
                    Reporter.ToUser(eUserMsgKey.RegistryValuesCheckFailed);
                }
                return(false);
            }
        }