Registry Tree : Registry Read « Windows « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows » Registry Read 
29.1.5.Registry Tree
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;

  public class RegistryBrowser : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TreeView tvRegistry;
    private System.Windows.Forms.ImageList ilTreeImages;

    public RegistryBrowser()
    {
      InitializeComponent();
      RootNodes();
    }
    private void InitializeComponent()
    {
      this.tvRegistry = new System.Windows.Forms.TreeView();
      this.ilTreeImages = new System.Windows.Forms.ImageList();
      this.SuspendLayout();
      // 
      // tvRegistry
      // 
      this.tvRegistry.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tvRegistry.Name = "tvRegistry";
      this.tvRegistry.Size = new System.Drawing.Size(392333);
      this.tvRegistry.TabIndex = 0;
      this.tvRegistry.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvRegistry_BeforeExpand);
      // 
      // ilTreeImages
      // 
      this.ilTreeImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilTreeImages.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // RegistryBrowser
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(392333);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.tvRegistry});
      this.Name = "RegistryBrowser";
      this.Text = "Registry Browser";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new RegistryBrowser());
    }

    private void tvRegistry_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
    {
      tvRegistry.BeginUpdate();
      foreach (TreeNode tn in e.Node.Nodes)
      {
        AddBranch(tn);
      }
      tvRegistry.EndUpdate();
    }

    public void AddBranch(TreeNode tn)
    {
      tn.Nodes.Clear();
      string strRegistryPath = tn.FullPath;

      RegistryKey regBranch = null;
      if (strRegistryPath.StartsWith("HKEY_CLASSES_ROOT"))
        regBranch = Registry.ClassesRoot;
      else if (strRegistryPath.StartsWith("HKEY_CURRENT_USER"))
        regBranch = Registry.CurrentUser;
      else if (strRegistryPath.StartsWith("HKEY_LOCAL_MACHINE"))
        regBranch = Registry.LocalMachine;
        else if (strRegistryPath.StartsWith("HKEY_USERS"))
        regBranch = Registry.Users;

      RegistryKey regSubKey = null;
      try
      {
        if (null != tn.Parent)
        {
          // we need the path minus the top level tree
          int nPosPathSeparator = strRegistryPath.IndexOf(this.tvRegistry.PathSeparator);
          string strSubkey = strRegistryPath.Substring(nPosPathSeparator+1);
          regSubKey = regBranch.OpenSubKey(strSubkey);
        }
        else
          regSubKey = regBranch;
      }
      catch
      {
        return;
      }

      string[] astrSubkeyNames = regSubKey.GetSubKeyNames();
      for (int i=0; i < astrSubkeyNames.Length; i++)
      {
        TreeNode tnBranch = new TreeNode(astrSubkeyNames[i],0,1);
        tn.Nodes.Add(tnBranch);
      }
    }

    public void RootNodes()
    {
      tvRegistry.BeginUpdate();

      TreeNode tnHKCR = new TreeNode("HKEY_CLASSES_ROOT",0,1);
      tvRegistry.Nodes.Add(tnHKCR);
      AddBranch(tnHKCR);

      TreeNode tnHKCU = new TreeNode("HKEY_CURRENT_USER",0,1);
      tvRegistry.Nodes.Add(tnHKCU);
      AddBranch(tnHKCU);

      TreeNode tnHKLM = new TreeNode("HKEY_LOCAL_MACHINE",0,1);
      tvRegistry.Nodes.Add(tnHKLM);
      AddBranch(tnHKLM);

      TreeNode tnHKU = new TreeNode("HKEY_USERS",0,1);
      tvRegistry.Nodes.Add(tnHKU);
      AddBranch(tnHKU);

      tvRegistry.SelectedNode = tnHKLM;
      tvRegistry.EndUpdate();
    }
  }
29.1.Registry Read
29.1.1.Get int value from Registry
29.1.2.Get Registry values with default
29.1.3.Loop through all subkeys contained in the current key
29.1.4.Loop through the values inside a key and display
29.1.5.Registry Tree
29.1.6.Registry Tree With Class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.