// ------------------------------------------------------------------ // Cree le node racine dans le RegistryTree. // Retourne Null si la racine est un WrongNode (sans enfants) // ------------------------------------------------------------------ private RegistryItem CreateRootNode(string rootpath) { if (rootpath == string.Empty) { // Vérification : HKLM RegistryItem WrongNode = new RegistryItem("HKLM is not allowed. Try a smaller subtree.", "node"); RegistryTree.Add(WrongNode); return(null); } else if (rootpath.Equals("SECURITY")) { // Vérification : HKLM\SECURITY RegistryItem WrongNode = new RegistryItem("SECURITY subtree is not accessible (reserved access).", "node"); RegistryTree.Add(WrongNode); return(null); } else { // Les vérifications sont OK: On crée le node Racine RegistryItem RacineNode = new RegistryItem(rootpath, "node"); RegistryTree.Add(RacineNode); this.AddToNodeTable(RacineNode, RacineNode.Name); // On memorise le Level de ce Node base.RacineNodeLevel = rootpath.Split('\\').Length; return(RacineNode); } }
// ------------------------------------------------------------------ // Cree les Nodes SubKeys et les Values du Node donné // C'est un peut redondant de donner le node et le path, mais ça évite de le recalculer à chaque fois. // ------------------------------------------------------------------ private void CreateChildNodes(RegistryItem ParentNode, string ParentPath) { RegistryKey rk; if (ParentNode == null) { return; } try { rk = Registry.LocalMachine.OpenSubKey(ParentPath); } catch (Exception ex) { RegistryItem WrongNode = new RegistryItem("This registry cannot be read (" + ex.Message + ").", "node"); RegistryTree.Add(WrongNode); return; } if (rk == null) { RegistryItem WrongNode = new RegistryItem("This registry was found null (" + ParentPath + ").", "node"); RegistryTree.Add(WrongNode); return; } string[] SubKeysArray = rk.GetSubKeyNames(); string[] ValuesArray = rk.GetValueNames(); // Traitement des Subkeys: on les ajoute dans l'arborescence foreach (string SubKeyName in SubKeysArray) { // on cree un nouveau node pour chaque SubKey string nodePath = ParentPath + @"\" + SubKeyName; RegistryItem NewNode = base.CreateRegistryNode(nodePath); // On met le node dans le dictionnaire base.AddToNodeTable(NewNode, SubKeyName); // On le rattache à son parent base.AttachToParentNode(NewNode, ParentNode); // On traite ses enfants (recursivité) this.CreateChildNodes(NewNode, nodePath); } // Traitement des Values: on les ajoute dans l'arborescence foreach (string ValueName in ValuesArray) { // On recupère toutes les infos sur cette value string ValueKind = string.Empty; string Value = string.Empty; try { ValueKind = rk.GetValueKind(ValueName).ToString(); } catch (NullReferenceException) { ValueKind = string.Empty; } try { Value = rk.GetValue(ValueName).ToString(); } catch (NullReferenceException) { Value = string.Empty; } // On cree une Key RegistryItem newKey = this.CreateRegistryKeyFromHive(ValueName, ValueKind, Value); // On la rattache à son parent base.AttachToParentNode(newKey, ParentNode); } }
// ------------------------------------------------------------------ // Parse le fichier REG // Enregistre l'arborescence dans un RegistryTree // Enregistre la liste des Nodes dans un Dictionnaire // ------------------------------------------------------------------ public void ParseFile(string fileName) { // On commence par vider la collection et le dictionnaire InitParser(); AverageLabelLength = 0; ModalLabelLength = 0; Array.Clear(TableStats, 0, TableStats.Length); // Vérification if (!fileName.EndsWith(".reg")) { RegistryItem WrongNode = new RegistryItem("Not a REG file", "node"); RegistryTree.Add(WrongNode); return; } // On lit le fichier et on met tout dans une très longue string. // fileName = "E:\\source\\repos\\RegFineViewer\\_example1.reg"; StreamReader streamFile = new StreamReader(File.Open(fileName, FileMode.OpenOrCreate)); string fichier = streamFile.ReadToEnd(); streamFile.Close(); // On decoupe le fichier en un tableau de lignes string[] lignes = fichier.Split('\r', '\n'); // Vérification if (lignes.Length > 50000) { RegistryItem WrongNode = new RegistryItem("The file contains too many lines (max 10.000).", "node"); RegistryTree.Add(WrongNode); return; } // On crée un Node Racine RegistryItem currentNode = new RegistryItem("root", "node"); RegistryTree.Add(currentNode); bool firstNode = true; // On parcourt le tableau des lignes du fichier for (int i = 0; i < lignes.Length; i++) { string ligne = lignes[i]; // On ignore la ligne d'entete if (ligne.StartsWith("Windows Registry Editor")) { } // On ignore les lignes vides else if (ligne.Equals("")) { } // Lignes de type [path\to\node] else if (ligne.StartsWith("[") && ligne.EndsWith("]")) { string nodePath = ligne.Trim('[', ']'); string parentPath = GetParentPath(nodePath); // ---------------------------------------------------------------------- // S'il s'agit du premier node // ---------------------------------------------------------------------- // Le node racine du treeView est le parent du premier node du fichier REG if (firstNode) { if (parentPath != "") { // Pour ce node racine, son nom est le chemin complet du parent currentNode.Name = parentPath; } else { // sauf si le nom du parent est vide... currentNode.Name = nodePath; } // On met le node Racine dans le dictionnaire AddToNodeTable(currentNode, currentNode.Name); // On memorise le Level de ce Node RacineNodeLevel = nodePath.Split('\\').Length - 1; firstNode = false; } // on cree un nouveau node currentNode = CreateRegistryNode(nodePath); // On le rattache à son parent AttachToParentNode(currentNode, parentPath); // on comptabilise sa longueur dans les statistiques TableStats[currentNode.Name.Length] += 1; } // Lignes du type: "ErrorLogSizeInKb" = dword:000186A0 // Lignes du type: "Application" = "" else if (ligne.StartsWith("\"")) { // Si on n'a pas de node courant, on passe. Ca ne devrait pas arriver. if (currentNode != null) { // On cree une Key RegistryItem newKey = CreateRegistryKeyFromFileLine(ligne); // On la rattache au Node courant currentNode.AddSubItem(newKey); } } // Autres cas else { } } }