Ejemplo n.º 1
0
 static RegEntry GetRegRecursive(string key, string name, string grepKey, string grepAttr)
 {
     try
     {
         var entry = new RegEntry(name);
         entry.Path = key;
         RegistryKey rParentKey =
             Registry.LocalMachine.OpenSubKey(key);
         string[] arySubKeyNames = rParentKey.GetSubKeyNames();
         //すべての属性を読み込む
         var attributes = rParentKey.GetValueNames();
         foreach (var attr in attributes)
         {
             var value = rParentKey.GetValue(attr);
             entry.Put(attr, new RegValue(rParentKey.GetValueKind(attr), value));
         }
         rParentKey.Close();
         //サブエントリを読み込む
         foreach (string subKeyName in arySubKeyNames)
         {
             var subentry = GetRegRecursive(key + "\\" + subKeyName, subKeyName, grepKey, grepAttr);
             if (subentry.GrepKey(grepKey) || subentry.GrepAttr(grepAttr))
             {
                 entry.Add(subentry);
             }
         }
         return(entry);
     }
     catch (System.Security.SecurityException se)
     {
         return(RegEntry.Error(key));
     }
 }
Ejemplo n.º 2
0
        static void Load(string path)
        {
            var regs  = new List <RegEntry>();
            var lines = File.ReadLines(path).ToArray();

            for (int i = 0; i < lines.Length; i++)
            {
                //@line
                var line = lines[i];
                //空行ならスキップ
                if (line.Length == 0)
                {
                    continue;
                }
                if (line != "@name")
                {
                    Console.WriteLine("invalid line: " + line + "<" + i + ">");
                    break;
                }
                //@line
                //key
                i++;
                line = lines[i];
                var key = line;
                var reg = new RegEntry(key);
                //@begin-attr-list
                i++;
                line = lines[i];
                if (line != "@begin-attr-list")
                {
                    Console.WriteLine("invalid line: " + line + "<" + i + ">");
                    break;
                }
                while (true)
                {
                    i++;
                    line = lines[i];
                    if (line == "@end-attr-list")
                    {
                        regs.Add(reg);
                        break;
                    }
                    //@begin-attr
                    if (line != "@begin-attr")
                    {
                        Console.WriteLine("invalid line: " + line + "<" + i + ">");
                        return;
                    }
                    //attr
                    i++;
                    line = lines[i];
                    var attr = line;
                    //type
                    i++;
                    line = lines[i];
                    var type = line;
                    //value
                    i++;
                    line = lines[i];
                    var value = line;
                    //@end-attr
                    i++;
                    line = lines[i];
                    if (line != "@end-attr")
                    {
                        Console.WriteLine("invalid line: " + line + "<" + i + ">");
                        return;
                    }
                    reg.Put(attr, new RegValue((RegistryValueKind)Enum.Parse(typeof(RegistryValueKind), type), value));
                }
            }
            var saveList    = new List <string>();
            var discardList = new List <string>();

            foreach (var reg in regs)
            {
                RegistryKey rParentKey =
                    Registry.LocalMachine.OpenSubKey(reg.Key, true);
                if (rParentKey == null)
                {
                    continue;
                }
                foreach (var key in reg.Keys())
                {
                    var regValue  = reg.Get(key);
                    var loadValue = (string)regValue.Value;
                    var realValue = rParentKey.GetValue(key);
                    saveList.Add(reg.Key + " " + key);
                    switch (regValue.Kind)
                    {
                    case RegistryValueKind.Binary:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    case RegistryValueKind.DWord:
                        int loadIvalue = (int)int.Parse(loadValue);
                        int readIvalue = (int)realValue;
                        //変更されていない
                        if (loadIvalue == readIvalue)
                        {
                            discardList.Add(saveList[saveList.Count - 1]);
                            saveList.RemoveAt(saveList.Count - 1);
                            break;
                        }
                        Console.WriteLine(reg.Key + " " + key);
                        Console.WriteLine(realValue + " -> " + loadIvalue);
                        Console.WriteLine("変更してもよろしいですか?(y/n):");
                        var rline = Console.ReadLine();
                        if (rline.ToLower() == "y" || rline.ToLower() == "yes")
                        {
                            rParentKey.SetValue(key, loadIvalue, RegistryValueKind.DWord);
                        }
                        else
                        {
                            discardList.Add(saveList[saveList.Count - 1]);
                            saveList.RemoveAt(saveList.Count - 1);
                        }
                        break;

                    case RegistryValueKind.ExpandString:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    case RegistryValueKind.MultiString:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    case RegistryValueKind.None:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    case RegistryValueKind.QWord:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    case RegistryValueKind.String:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    case RegistryValueKind.Unknown:
                        discardList.Add(saveList[saveList.Count - 1]);
                        saveList.RemoveAt(saveList.Count - 1);
                        break;

                    default:
                        break;
                    }
                }
                rParentKey.Close();
            }
            if (saveList.Count > 0)
            {
                Console.WriteLine("変更されなかった一覧");
                foreach (var e in discardList)
                {
                    Console.WriteLine("    " + e);
                }
                Console.WriteLine("変更された一覧");
                foreach (var e in saveList)
                {
                    Console.WriteLine("     " + e);
                }
            }
            else
            {
                Console.WriteLine("ダンプファイルが変更されていないか、全ての変更が無視されました。");
            }
        }