Esempio n. 1
0
        public void Load(string fullPath)
        {
            using (StreamReader reader = new StreamReader(fullPath))
            {
                // Read the first line to determine the format.

                string line = reader.ReadLine();
                if (line != "REGEDIT4" && line != "Windows Registry Editor Version 5.00")
                {
                    throw new System.ApplicationException("The first line of the registry file is '" + line + "', but the expected first line is 'REGEDIT4' or 'Windows Registry Editor Version 5.00'.");
                }

                // REGEDIT version 4 saves expandable strings using ANSI encoding, but version 5 uses Unicode encoding.

                bool unicode = line == "Windows Registry Editor Version 5.00";
                RegistryCaptureKey currentKey = null;

                line = reader.ReadLine();

                for ( ; ;)
                {
                    if (line == null)
                    {
                        break;
                    }

                    Match match;
                    if ((match = m_keyRegex.Match(line)).Success)
                    {
                        currentKey = LoadKey(match);
                        if (currentKey != null)
                        {
                            m_keys.Add(currentKey.Path, currentKey);
                        }
                        line = reader.ReadLine();
                    }
                    else if ((match = m_defaultValueRegex.Match(line)).Success)
                    {
                        RegistryCaptureValue value = LoadDefaultValue(match);
                        if (currentKey != null && value != null)
                        {
                            currentKey.Add(value);
                        }
                        line = reader.ReadLine();
                    }
                    else if ((match = m_valueRegex.Match(line)).Success)
                    {
                        RegistryCaptureValue value = LoadValue(match, unicode, reader, ref line);
                        if (currentKey != null && value != null)
                        {
                            currentKey.Add(value);
                        }
                    }
                    else
                    {
                        line = reader.ReadLine();
                    }
                }
            }
        }
Esempio n. 2
0
 private string GetValue(RegistryCaptureValue value)
 {
     return(value.Value == null ? string.Empty : value.Value.ToString());
 }
Esempio n. 3
0
 public void Add(RegistryCaptureValue value)
 {
     m_values[value.Name] = value;
 }
Esempio n. 4
0
        private RegistryCaptureValue LoadValue(Match match, bool unicode, StreamReader reader, ref string line)
        {
            RegistryCaptureValue registryValue = null;
            string name  = match.Groups["Name"].Value;
            string value = match.Groups["Value"].Value;

            if ((match = m_stringValueRegex.Match(value)).Success)
            {
                // Unescape.

                value         = Regex.Replace(match.Groups["Value"].Value, @"\\(.)", "$1");
                registryValue = new RegistryCaptureStringValue(name, value);
            }
            else if ((match = m_dwordValueRegex.Match(value)).Success)
            {
                int dwordValue = System.Convert.ToInt32(match.Groups["Value"].Value, 16);
                registryValue = new RegistryCaptureDWordValue(name, dwordValue);
            }
            else if ((match = m_binaryValueRegex.Match(value)).Success)
            {
                byte[] bytes = null;
                GetBytes(match, ref bytes, reader, ref line);
                registryValue = new RegistryCaptureBinaryValue(name, bytes);
            }
            else if ((match = m_expandableValueRegex.Match(value)).Success)
            {
                byte[] bytes = null;
                GetBytes(match, ref bytes, reader, ref line);
                Encoding encoding = unicode ? Encoding.Unicode : Encoding.ASCII;
                registryValue = new RegistryCaptureExpandStringValue(name, encoding.GetString(bytes));
            }
            else if ((match = m_multiStringValueRegex.Match(value)).Success)
            {
                byte[] bytes = null;
                GetBytes(match, ref bytes, reader, ref line);
                Encoding encoding = unicode ? Encoding.Unicode : Encoding.ASCII;
                string[] strings  = encoding.GetString(bytes).Split('\0');

                // Remove any empty strings from the end.

                LinkedList <string>     stringList = new LinkedList <string>(strings);
                LinkedListNode <string> node       = stringList.Last;
                while (node != null)
                {
                    if (!string.IsNullOrEmpty(node.Value))
                    {
                        break;
                    }
                    stringList.Remove(node);
                    node = stringList.Last;
                }

                strings = new string[stringList.Count];
                stringList.CopyTo(strings, 0);

                registryValue = new RegistryCaptureMultiStringValue(name, strings);
            }

            line = reader.ReadLine();
            return(registryValue);
        }