public RegistryValue(RegistryRoot root, string valueName, string keyName, object value)
 {
     _root      = root;
     _value     = value;
     _valueName = valueName;
     _keyName   = keyName;
 }
Example #2
0
        public static RegistryKey WriteRegistryPath(RegistryRoot registryRoot, string completePath)
        {
            var Paths = completePath.Split('\\');

            var currentKey = GetRegistryRoot(registryRoot);

            RegistryKey currentCandidate;

            if (currentKey == null)
            {
                throw new InvalidOperationException("Failed to retrieve root key");
            }

            foreach (var path in Paths)
            {
                currentCandidate = currentKey.OpenSubKey(path, true);
                if (currentCandidate == null)
                {
                    currentCandidate = currentKey.CreateSubKey(path);
                }

                currentKey = currentCandidate;
            }

            if (currentKey == null)
            {
                throw new InvalidOperationException(string.Format("Unable to create the path {0}\\{1}", registryRoot, completePath));
            }

            return(currentKey);
        }
Example #3
0
 public void WriteKey(RegistryRoot root, string registryKey)
 {
     this.msixRegistryFacade.Add(new RegistryEntry
     {
         Key  = registryKey,
         Root = root
     });
 }
Example #4
0
 public void WriteValue(RegistryRoot root, string key, string name, ValueType type, object value)
 {
     this.msixRegistryFacade.Add(new RegistryEntry
     {
         Key   = key,
         Name  = name,
         Root  = root,
         Type  = type,
         Value = value
     });
 }
Example #5
0
        public RegistryInstallerElement(RegistryRoot root, [NotNull] string path, string name,
                                        params RegistryValue[] value)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            _root = root;
            Path  = path;
            Value = value;
            _name = name;
        }
        public void Install()
        {
            VariableCollection variables = PrepareVariables();
            RegistryRoot       registry  = RegistryRoot.Parse(Properties.Resources.HKLMRegistry);

            if (!TryRegister(registry, variables))
            {
                registry = RegistryRoot.Parse(Properties.Resources.HKCURegistry);
                if (!TryRegister(registry, variables))
                {
                    throw new Exception("Unable to register .NET Profiler");
                }
            }
            _registry = registry;
        }
Example #7
0
        /// <summary>
        /// Inicializa una nueva instancia de la clase <see cref="RegistryIdentity" />
        /// </summary>
        /// <param name="root">Clave en el registro de Windows donde se incia la búsqueda.</param>
        /// <param name="subKeyPath">El nombre o la ruta de la subclave donde se buscan los valores.</param>
        /// <param name="apiKeyName">El nombre de la entrada en donde se busca el valor del ApiKey. Esta cadena no distingue entre mayúsculas y minúsculas.</param>
        /// <param name="apiSecretName">El nombre de la entrada en donde se busca el valor del ApiSecret.</param>
        public RegistryIdentity(
            RegistryRoot root    = RegistryRoot.LocalMachine,
            string subKeyPath    = @"SOFTWARE\Aspen\Credentials",
            string apiKeyName    = "APIKEY",
            string apiSecretName = "APISECRET")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(subKeyPath))
                {
                    throw new ArgumentNullException(nameof(subKeyPath));
                }

                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    throw new PlatformNotSupportedException();
                }

                RegistryKey rootKey      = root.ToRegistryKey();
                string      registryPath = $@"{rootKey}\{subKeyPath}";
                using (RegistryKey registryKey = rootKey.OpenSubKey(subKeyPath))
                {
                    if (registryKey == null)
                    {
                        return;
                    }

                    this.ApiKey    = registryKey.GetValue(apiKeyName)?.ToString()?.TryDecrypt();
                    this.ApiSecret = registryKey.GetValue(apiSecretName)?.ToString()?.TryDecrypt();

                    if (string.IsNullOrWhiteSpace(this.ApiKey))
                    {
                        throw new IdentityException($@"Value for ApiKey not found in Registry:{registryPath} => {apiKeyName}");
                    }

                    if (string.IsNullOrWhiteSpace(this.ApiSecret))
                    {
                        throw new IdentityException($@"Value for ApiSecret not found in Registry:{registryPath} => {apiSecretName}");
                    }
                }
            }
            catch (Exception exception)
            {
                throw new IdentityException(exception);
            }
        }
Example #8
0
        /// <summary>
        /// Convierte <paramref name="root"/> en su representación de <see cref="RegistryKey"/>.
        /// </summary>
        /// <param name="root">Valor a convertir.</param>
        /// <returns>Instancia de <see cref="RegistryKey"/> que representa <paramref name="root"/>.</returns>
        public static RegistryKey ToRegistryKey(this RegistryRoot root)
        {
            switch (root)
            {
            case RegistryRoot.LocalMachine:
                return(Registry.LocalMachine);

            case RegistryRoot.CurrentUser:
                return(Registry.CurrentUser);

            case RegistryRoot.ClassesRoot:
                return(Registry.ClassesRoot);

            default:
                throw new InvalidEnumArgumentException(nameof(root), (int)root, typeof(RegistryRoot));
            }
        }
        public static string ToMsixRegistryPath(RegistryRoot root, string path)
        {
            switch (root)
            {
            case RegistryRoot.HKEY_CLASSES_ROOT:
                return("REGISTRY\\MACHINE\\Software\\Classes\\" + path);

            case RegistryRoot.HKEY_CURRENT_USER:
                return("REGISTRY\\USER\\[{CurrentUserSID}]\\" + path);

            case RegistryRoot.HKEY_USERS:
                return("REGISTRY\\USER\\" + path);

            default:
                return("REGISTRY\\MACHINE\\" + path);
            }
        }
        public static RegistryAccessorReportData GetPropertyData(RegistryRoot registryRoot, AccessorTypeDeclaration accessorTypeDeclaration, AccessorFieldDeclaration accessorFieldDeclaration)
        {
            var assemblyName  = accessorTypeDeclaration.TargetInterfaceType.Assembly.GetName().Name;
            var interfaceName = accessorTypeDeclaration.TargetInterfaceType.Name;

            var registoKeyAttribute = accessorTypeDeclaration.GetAttribute <RegistryKeyAttribute>();
            var key = string.IsNullOrEmpty(registoKeyAttribute?.Key)
                ? $@"Software\ApplicationRegistries\{assemblyName}\{interfaceName}"
                : registoKeyAttribute.Key;

            var registoNameAttribute = accessorFieldDeclaration.GetAttribute <RegistryNameAttribute>();
            var name = string.IsNullOrEmpty(registoNameAttribute?.Name)
                ? accessorFieldDeclaration.Name
                : registoNameAttribute.Name;

            return(new RegistryAccessorReportData(key,
                                                  name
                                                  ));
        }
 private bool TryRegister(RegistryRoot registry, VariableCollection variables)
 {
     try
     {
         registry.Remove();
         registry.Import(variables);
         return(true);
     }
     catch (Exception)
     {
         try
         {
             registry.Remove();
         }
         catch (Exception)
         {
         }
         return(false);
     }
 }
Example #12
0
        public static bool RegistryValueExists(RegistryRoot root, string subKey, string valueName)
        {
            RegistryKey registryKey;

            switch (root)
            {
            case RegistryRoot.HKLM:
                registryKey = Registry.LocalMachine.OpenSubKey(subKey, false);
                break;

            case RegistryRoot.HKCU:
                registryKey = Registry.CurrentUser.OpenSubKey(subKey, false);
                break;

            default:
                throw new System.InvalidOperationException(
                          "parameter subKey must be either \"HKLM\" or \"HKCU\"");
            }

            return(registryKey != null && registryKey.GetValue(valueName) != null);
        }
        public int AddRegValue(
            RegistryRoot registryRoot,
            string subkey,
            string valueName,
            string value,
            DestinationRegValueFlags flags)
        {
            var valueId = lastUsedValueId++;

            _regValues.Add(new DestinationRegValue
            {
                ValueId = valueId,
                RegistryRoot = registryRoot,
                Subkey = subkey,
                ValueName = valueName,
                Value = value,
                Flags = flags
            });

            return valueId;
        }
Example #14
0
        private static RegistryKey GetRegistryRoot(RegistryRoot registryRoot)
        {
            switch (registryRoot)
            {
            case RegistryRoot.HKEY_Classes_Root:
                return(Registry.ClassesRoot);

            case RegistryRoot.HKEY_Current_User:
                return(Registry.CurrentUser);

            case RegistryRoot.HKEY_Local_Machine:
                return(Registry.LocalMachine);

            case RegistryRoot.HKEY_Users:
                return(Registry.Users);

            case RegistryRoot.HKEY_Current_Config:
                return(Registry.CurrentConfig);

            default:
                throw new ArgumentOutOfRangeException("registryRoot");
            }
        }
        /// <summary>
        ///		Obtiene la clave raíz
        /// </summary>
        private RegistryKey GetRegistryRoot(RegistryRoot intRoot)
        {
            switch (intRoot)
            {
            case RegistryRoot.ClassesRoot:
                return(Registry.ClassesRoot);

            case RegistryRoot.CurrentConfig:
                return(Registry.CurrentConfig);

            case RegistryRoot.CurrentUser:
                return(Registry.CurrentUser);

            case RegistryRoot.LocalMachine:
                return(Registry.LocalMachine);

            case RegistryRoot.PerformanceData:
                return(Registry.PerformanceData);

            case RegistryRoot.Users:
            default:
                return(null);
            }
        }
Example #16
0
        /// <summary>
        /// Inicializa una nueva instancia de la clase <see cref="RegistryEndpoint"/>
        /// </summary>
        /// <param name="root">Clave en el registro de Windows donde se incia la búsqueda.</param>
        /// <param name="subKeyPath">El nombre o la ruta de la subclave donde se buscan los valores.</param>
        /// <param name="urlName">El nombre de la entrada en donde se busca el valor de la URL. Esta cadena no distingue entre mayúsculas y minúsculas.</param>
        /// <param name="timeoutName">El nombre de la entrada en donde se busca el valor del tiempo de espera.</param>
        public RegistryEndpoint(
            RegistryRoot root  = RegistryRoot.LocalMachine,
            string subKeyPath  = @"SOFTWARE\Aspen\Credentials",
            string urlName     = "SERVICE_URL",
            string timeoutName = "SERVICE_TIMEOUT")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(subKeyPath))
                {
                    throw new ArgumentNullException(nameof(subKeyPath));
                }

                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    throw new PlatformNotSupportedException();
                }

                RegistryKey rootKey = root.ToRegistryKey();

                using (RegistryKey registryKey = rootKey.OpenSubKey(subKeyPath))
                {
                    if (registryKey == null)
                    {
                        return;
                    }

                    this.SetUrl(registryKey.GetValue(urlName)?.ToString());
                    this.SetTimeout(registryKey.GetValue(timeoutName)?.ToString(), nameof(timeoutName));
                }
            }
            catch (Exception exception)
            {
                throw new IdentityException(exception);
            }
        }
Example #17
0
        private string GetRegistryPath(Session session, RegistryRoot root, string key, string name)
        {
            bool   allUsers = session.EvaluateCondition("ALLUSERS = 1", true);
            string rootName = "????";

            switch (root)
            {
            case RegistryRoot.LocalMachine: rootName = "HKLM"; break;

            case RegistryRoot.CurrentUser: rootName = "HKCU"; break;

            case RegistryRoot.Users: rootName = "HKU"; break;

            case RegistryRoot.UserOrMachine: rootName = (allUsers ? "HKLM" : "HKCU"); break;

            case RegistryRoot.ClassesRoot: rootName = (allUsers ? @"HKLM\Software\Classes" : @"HKCU\Software\Classes"); break;
                // TODO: Technically, RegistryRoot.ClassesRoot should be under HKLM on NT4.
            }
            if (name.Length == 0)
            {
                name = "(Default)";
            }
            if (name == "+" || name == "*")
            {
                name = "";
            }
            else
            {
                name = " : " + name;
            }
            using (Record formatRec = new Record(0))
            {
                formatRec[0] = String.Format(@"{0}\{1}{2}", rootName, key, name);
                return(session.FormatRecord(formatRec));
            }
        }
        /// <summary>
        ///		Asigna un valor a un elemento
        /// </summary>
        public void SetValue(RegistryRoot intRoot, string strKeyName, string strValueName, object objValue, RegistryValueKind intIDType)
        {
            RegistryKey objRegistryRoot = GetRegistryRoot(intRoot);

            // Crea la clave
            if (objRegistryRoot != null)
            {
                RegistryKey objKey = objRegistryRoot.CreateSubKey(strKeyName);

                // Asigna el valor
                if (objKey != null)
                {
                    Registry.SetValue(objKey.Name, strValueName, objValue, intIDType);
                }
                else
                {
                    throw new NotSupportedException("No se ha podido crea la subclave: " + strKeyName);
                }
            }
            else
            {
                throw new NotImplementedException("No se encuentra la raíz de la clave " + intRoot.ToString());
            }
        }
Example #19
0
 public static InstallationBuilder WithRegistryElement(this InstallationBuilder installationBuilder, RegistryRoot root, string path, string name, RegistryValue[] values)
 {
     return(installationBuilder.WithElement(new RegistryInstallerElement(root, path, name, values)));
 }
 internal RegistoryAccessor(RegistryRoot registryRoot)
 {
     _registryRoot = registryRoot;
 }
		/// <summary>
		///		Asigna un valor a un elemento
		/// </summary>
		public void SetValue(RegistryRoot intRoot, string strKeyName, string strValueName, object objValue, RegistryValueKind intIDType)
		{	RegistryKey objRegistryRoot = GetRegistryRoot(intRoot);

				// Crea la clave
					if (objRegistryRoot != null)
						{ RegistryKey objKey = objRegistryRoot.CreateSubKey(strKeyName);

								// Asigna el valor
									if (objKey != null)
										Registry.SetValue(objKey.Name, strValueName, objValue, intIDType);
									else
										throw new NotSupportedException("No se ha podido crea la subclave: " + strKeyName);
						}
					else
						throw new NotImplementedException("No se encuentra la raíz de la clave " + intRoot.ToString());
		}
Example #22
0
        public static bool RegistryValueExists(RegistryRoot root, string subKey, string valueName)
        {
            RegistryKey registryKey;
            switch (root)
            {
                case RegistryRoot.HKLM:
                    registryKey = Registry.LocalMachine.OpenSubKey(subKey, false);
                    break;
                case RegistryRoot.HKCU:
                    registryKey = Registry.CurrentUser.OpenSubKey(subKey, false);
                    break;
                default:
                    throw new System.InvalidOperationException(
                        "parameter subKey must be either \"HKLM\" or \"HKCU\"");
            }

            return registryKey != null && registryKey.GetValue(valueName) != null;
        }
		/// <summary>
		///		Obtiene la clave raíz
		/// </summary>
		private RegistryKey GetRegistryRoot(RegistryRoot intRoot)
		{ switch (intRoot)
				{	case RegistryRoot.ClassesRoot:
						return Registry.ClassesRoot;
					case RegistryRoot.CurrentConfig:
						return Registry.CurrentConfig;
					case RegistryRoot.CurrentUser:
						return Registry.CurrentUser;
					case RegistryRoot.LocalMachine:
						return Registry.LocalMachine;
					case RegistryRoot.PerformanceData:
						return Registry.PerformanceData;
					case RegistryRoot.Users:
					default:
						return null;
				}
		}
Example #24
0
        public static void DeleteRegistryPath(RegistryRoot hkeyLocalMachine, string generateRegistryPath)
        {
            var root = GetRegistryRoot(hkeyLocalMachine);

            root.DeleteSubKeyTree(generateRegistryPath, false);
        }
Example #25
0
 public RegistryHelper(RegistryRoot root, string path)
 {
     Root = root;
     Path = path;
 }
Example #26
0
 private string GetRegistryPath(Session session, RegistryRoot root, string key, string name)
 {
     bool allUsers = session.EvaluateCondition("ALLUSERS = 1", true);
     string rootName = "????";
     switch(root)
     {
         case RegistryRoot.LocalMachine : rootName = "HKLM"; break;
         case RegistryRoot.CurrentUser  : rootName = "HKCU"; break;
         case RegistryRoot.Users        : rootName = "HKU"; break;
         case RegistryRoot.UserOrMachine: rootName = (allUsers ? "HKLM" : "HKCU"); break;
         case RegistryRoot.ClassesRoot  : rootName = (allUsers ? @"HKLM\Software\Classes" : @"HKCU\Software\Classes"); break;
         // TODO: Technically, RegistryRoot.ClassesRoot should be under HKLM on NT4.
     }
     if(name.Length == 0) name = "(Default)";
     if(name == "+" || name == "*") name = "";
     else name = " : " + name;
     using(Record formatRec = new Record(0))
     {
         formatRec[0] = String.Format(@"{0}\{1}{2}", rootName, key, name);
         return session.FormatRecord(formatRec);
     }
 }