using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
namespace RegistryBrowser
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.treeView1 = new System.Windows.Forms.TreeView();
this.splitter1 = new System.Windows.Forms.Splitter();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// panel1
//
this.panel1.Controls.Add(this.treeView1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 254);
this.panel1.TabIndex = 0;
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.ImageList = this.imageList1;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(200, 254);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(200, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(8, 254);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(208, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(232, 254);
this.listView1.SmallImageList = this.imageList1;
this.listView1.TabIndex = 2;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "名称";
this.columnHeader1.Width = 91;
//
// columnHeader2
//
this.columnHeader2.Text = "类型";
this.columnHeader2.Width = 86;
//
// columnHeader3
//
this.columnHeader3.Text = "数据";
this.columnHeader3.Width = 2401;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(440, 254);
this.Controls.Add(this.listView1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "演示浏览注册表信息";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{//添加注册表五个主根键
TreeNode RootNode=new TreeNode("我的电脑",0,2);
this.treeView1.Nodes.Add(RootNode);
TreeNode SubNode1=new TreeNode("HKEY_CLASSES_ROOT",1,0);
RootNode.Nodes.Add(SubNode1);
TreeNode SubNode2=new TreeNode("HKEY_CURRENT_USER",1,0);
RootNode.Nodes.Add(SubNode2);
TreeNode SubNode3=new TreeNode("HKEY_LOCAL_MACHINE",1,0);
RootNode.Nodes.Add(SubNode3);
TreeNode SubNode4=new TreeNode("HKEY_USERS",1,0);
RootNode.Nodes.Add(SubNode4);
TreeNode SubNode5=new TreeNode("HKEY_CURRENT_CONFIG",1,0);
RootNode.Nodes.Add(SubNode5);
}
private RegistryKey GetRegKey(TreeNode node)
{//取得注册表节点信息
string MyPath=node.FullPath;
RegistryKey MyReg=Registry.LocalMachine;
//取得五个注册表主键
if (MyPath.Length>4)
{
string[] KeyName = MyPath.Split(new Char[]{'//'});
switch (KeyName[1])
{
case "HKEY_CLASSES_ROOT" :
MyReg=Registry.ClassesRoot;
break;
case "HKEY_CURRENT_USER" :
MyReg=Registry.CurrentUser;
break;
case "HKEY_LOCAL_MACHINE" :
MyReg=Registry.LocalMachine;
break;
case "HKEY_USERS" :
MyReg=Registry.Users;
break;
case "HKEY_CURRENT_CONFIG" :
MyReg=Registry.CurrentConfig;
break;
}
//去掉“我的电脑”
MyPath=MyPath.Substring(6);
if (MyPath.IndexOf("//")>0)
{
MyPath=MyPath.Substring(MyPath.IndexOf("//")+1);
try
{
MyReg=MyReg.OpenSubKey(MyPath);
}
catch(Exception e)
{
;
}
}
}
return MyReg;
}
private void AddSubRegKey(TreeNode node)
{//增加注册表新节点
if (node.Nodes.Count==0)
{
RegistryKey MyReg=GetRegKey(node);
string[] MySubKey=MyReg.GetSubKeyNames();
//所有子键名加入TreeView
for (int i=0;i<MySubKey.Length;i++)
{
node.Nodes.Add(MySubKey[i]);
}
}
}
private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{//添加注册表子节点
for (int i=0;i<this.treeView1.SelectedNode.Nodes.Count;i++)
{
try
{
this.AddSubRegKey(e.Node.Nodes[i]);
}
catch (Exception ee)
{
;
}
}
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{//在列表视图填充注册表键值数据
this.listView1.Items.Clear();
RegistryKey MyReg=GetRegKey(this.treeView1.SelectedNode);
string[] Values=MyReg.GetValueNames();
string MyRegValueName;
object MyRegValueType,MyRegValueData;
for (int i=0;i<Values.Length;i++)
{
MyRegValueName=Values[i];
MyRegValueData=MyReg.GetValue(MyRegValueName);
MyRegValueType=MyRegValueData.GetType();
ListViewItem MySubItem;
if (MyRegValueType.ToString()=="System.String")
{
MyRegValueType="REG_SZ";
MySubItem=new ListViewItem(new String[]{MyRegValueName,MyRegValueType.ToString(),MyRegValueData.ToString()},3);
}
else
{
MyRegValueType="REG_DWORD";
MySubItem=new ListViewItem(new String[]{MyRegValueName,MyRegValueType.ToString(),MyRegValueData.ToString()},4);
}
this.listView1.Items.Add(MySubItem);
}
}
}
}
如何浏览注册表信息
最新推荐文章于 2020-06-16 20:31:52 发布