在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 获取注册表信息(树形展示)

C# 获取注册表信息(树形展示)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.03M
  • 下载次数:32
  • 浏览次数:355
  • 发布时间:2015-10-12
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C# 注册表

实例介绍

【实例简介】
【实例截图】

【核心代码】


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);
			}			
		}
	}
}


标签: C# 注册表

实例下载地址

C# 获取注册表信息(树形展示)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警