Exemple #1
0
        /// Read registry string.
        /// This also supports a means to look for high-versioned keys by use
        /// of a $VERSION placeholder in the key path.
        /// $VERSION in the key path is a placeholder for the version number,
        /// causing the highest value path to be searched for and used.
        /// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
        /// There can be additional characters in the component.  Only the numeric
        /// characters are compared.
        static bool GetSystemRegistryString(string keyPath, string valueName,
                                            ICollection <ToolchainVersion> entries, RegistryView view)
        {
            string subKey;
            var    hive    = GetRegistryHive(keyPath, out subKey);
            var    rootKey = RegistryKey.OpenBaseKey(hive, view);

            var versionPosition = subKey.IndexOf("\\$VERSION", StringComparison.Ordinal);

            if (versionPosition <= 0)
            {
                return(false);
            }

            // If we have a $VERSION placeholder, do the highest-version search.
            var partialKey = subKey.Substring(0, versionPosition);

            using (var key = rootKey.OpenSubKey(partialKey, writable: false))
            {
                if (key == null)
                {
                    return(false);
                }

                foreach (var subKeyName in key.GetSubKeyNames())
                {
                    // Get the number version from the key.
                    var match = Regex.Match(subKeyName, @"[1-9][0-9]*\.?[0-9]*");
                    if (!match.Success)
                    {
                        continue;
                    }

                    var versionText = match.Groups[0].Value;

                    float version;
                    float.TryParse(versionText, NumberStyles.Number,
                                   CultureInfo.InvariantCulture, out version);

                    using (var versionKey = key.OpenSubKey(subKeyName))
                    {
                        // Check that the key has a value passed by the caller.
                        var keyValue = versionKey.GetValue(valueName);
                        if (keyValue == null)
                        {
                            continue; // Skip this version since it's invalid.
                        }
                        var entry = new ToolchainVersion
                        {
                            Version   = version,
                            Directory = keyValue.ToString()
                        };

                        entries.Add(entry);
                    }
                }
            }

            return(true);
        }
Exemple #2
0
        /// Read registry string.
        /// This also supports a means to look for high-versioned keys by use
        /// of a $VERSION placeholder in the key path.
        /// $VERSION in the key path is a placeholder for the version number,
        /// causing the highest value path to be searched for and used.
        /// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
        /// There can be additional characters in the component.  Only the numeric
        /// characters are compared.
        static bool GetSystemRegistryString(string keyPath, string valueName,
            ICollection<ToolchainVersion> entries, RegistryView view)
        {
            string subKey;
            var hive = GetRegistryHive(keyPath, out subKey);
            var rootKey = RegistryKey.OpenBaseKey(hive, view);

            var versionPosition = subKey.IndexOf("\\$VERSION", StringComparison.Ordinal);
            if (versionPosition <= 0)
                return false;

            // If we have a $VERSION placeholder, do the highest-version search.
            var partialKey = subKey.Substring(0, versionPosition);
            using (var key = rootKey.OpenSubKey(partialKey, writable: false))
            {
                if (key == null)
                    return false;

                foreach (var subKeyName in key.GetSubKeyNames())
                {
                    // Get the number version from the key.
                    var match = Regex.Match(subKeyName, @"[1-9][0-9]*\.?[0-9]*");
                    if (!match.Success)
                        continue;

                    var versionText = match.Groups[0].Value;

                    float version;
                    float.TryParse(versionText, NumberStyles.Number,
                        CultureInfo.InvariantCulture, out version);

                    using (var versionKey = key.OpenSubKey(subKeyName))
                    {
                        // Check that the key has a value passed by the caller.
                        var keyValue = versionKey.GetValue(valueName);
                        if (keyValue == null)
                            continue; // Skip this version since it's invalid.

                        var entry = new ToolchainVersion
                        {
                            Version = version,
                            Directory = keyValue.ToString()
                        };

                        entries.Add(entry);
                    }
                }
            }

            return true;
        }