Example #1
0
        private RegFileWrapper NewFileWrapper(RegFileInfo file, RegFileType type)
        {
            RegFileWrapper wrapper = null;

            if ((file != null) && (file.VerFlag != null))
            {
                wrapper = new RegFileWrapper(file)
                {
                    FileType = type
                };
                foreach (VersionInfo info in this.versionList)
                {
                    if ((info != null) && (info.Sign == file.VerFlag))
                    {
                        wrapper.DisplayName = info.Name;
                        wrapper.VersionDesc = info.Description;
                        wrapper.ExportFlag  = info.ExportFlag;
                    }
                }
            }
            return(wrapper);
        }
        /// <summary>
        /// Windows Registry Editor Version 5.00
        /// [<Hive name>\<Key name>\<Subkey name>]
        /// "Value name"=<Value type>:<Value data>
        /// </summary>
        /// <param name="fileName"></param>
        protected void ParseTextRegFile(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName) || (!File.Exists(fileName)))
            {
                return;
            }

            string        lastKey       = null;
            StringBuilder lastRegValue  = null;
            string        lastValueName = null;
            string        lastValueKind = null;
            int           lineCounter   = 0;

            using (TextReader reader = new StreamReader(fileName))
            {
                while (true)
                {
                    var item = reader.ReadLine();
                    lineCounter++;
                    //if (lineCounter == 14)
                    //{
                    //    Debugger.Break();
                    //}

                    if (item == null)
                    {
                        AddRegTextFileValue(lastKey, lastValueName, lastValueKind, lastRegValue);
                        break;
                    }
                    //as of now this is not supported
                    else if (item == REGEDIT4)
                    {
                        _fileType  = RegFileType.REGEDIT4;
                        codingType = StringExtensionMethods.CodingType.ASCII;
                        return;
                    }
                    else if (item == REGEDIT5)
                    {
                        _fileType  = RegFileType.REGEDIT5;
                        codingType = StringExtensionMethods.CodingType.Unicode;
                        continue;
                    }
                    //empty lines skip
                    else if (item == string.Empty)
                    {
                        AddRegTextFileValue(lastKey, lastValueName, lastValueKind, lastRegValue);
                        lastKey       = null;
                        lastRegValue  = null;
                        lastValueKind = null;
                        lastValueName = null;
                        continue;
                    }
                    //comment lines skip
                    else if (item[0] == ';')
                    {
                        continue;
                    }
                    //new key
                    else if (item[0] == '[')
                    {
                        AddRegTextFileValue(lastKey, lastValueName, lastValueKind, lastRegValue);
                        lastRegValue  = null;
                        lastValueKind = null;
                        lastValueName = null;
                        var tempKey = item.RemoveStartEnd('[', ']');
                        if (string.IsNullOrWhiteSpace(tempKey))
                        {
                            continue;
                        }
                        else if (tempKey.IndexOf(HKLM_LONG, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            lastKey = string.Format("{0}{1}", HKLM_SHORT, tempKey.Substring(HKLM_LONG.Length));
                        }
                        else if (tempKey.IndexOf(HKCU_LONG, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            lastKey = string.Format("{0}{1}", HKCU_SHORT, tempKey.Substring(HKCU_LONG.Length));
                        }
                        else
                        {
                            lastKey = tempKey;
                        }
                    }
                    //continue of last binary type value
                    else if (item.Length > 3 && item.IndexOf("  ") == 0 && item[2] != ' ')
                    {
                        if (lastRegValue != null && lastValueName != null &&
                            lastValueKind != null && lastKey != null)
                        {
                            string tempVal = item.Trim();
                            if (string.IsNullOrWhiteSpace(tempVal) || tempVal[tempVal.Length - 1] != '\\')
                            {
                                if (!string.IsNullOrWhiteSpace(tempVal))
                                {
                                    lastRegValue.Append(tempVal);
                                }
                                AddRegTextFileValue(lastKey, lastValueName, lastValueKind, lastRegValue);
                                lastRegValue  = null;
                                lastValueKind = null;
                                lastValueName = null;
                            }
                            else
                            {
                                lastRegValue.Append(tempVal.Substring(0, tempVal.Length - 1));
                            }
                        }
                    }
                    //reg default value start
                    else if (item.IndexOf("@=") == 0)
                    {
                        AddRegTextFileValue(lastKey, lastValueName, lastValueKind, lastRegValue);
                        lastRegValue  = new StringBuilder();
                        lastValueKind = null;
                        lastValueName = null;
                        ParseTextFileRegValue(item, out lastValueName, out lastValueKind, ref lastRegValue);
                    }
                    //reg value name value start
                    else if (item[0] == '"' && item.IndexOf("\"=") > 0)
                    {
                        AddRegTextFileValue(lastKey, lastValueName, lastValueKind, lastRegValue);
                        lastRegValue  = new StringBuilder();
                        lastValueKind = null;
                        lastValueName = null;
                        ParseTextFileRegValue(item, out lastValueName, out lastValueKind, ref lastRegValue);
                    }
                    // some corrupt / multiline value as REG_SZ / this is
                    else
                    {
                        if (lastRegValue != null && lastValueName != null &&
                            lastValueKind != null && lastKey != null)
                        {
                            lastRegValue.Append("\r\n");
                            lastRegValue.Append(item);
                        }
                    }
                }
            }
        }