⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#代码,注册表浏览器 主要是浏览注册表的键值.
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32 ;
namespace RegistryViewer
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView tVRegistryKey;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ListView lVRegistryValue;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ImageList imageList1;
		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 );
		}
		
		private RegistryKey GetKeyByNode(TreeNode node)
		{
			string path=node.FullPath;
			RegistryKey rk=Registry.LocalMachine;
			//不是根节点(我的电脑)
			if(path.Length>4)
			{
				string[] tokens=path.Split(new Char[]{'\\'});
				//判断node代表注册键属于哪一个根键
				switch(tokens[1])
				{
					case"HKEY_CLASSES_ROOT":
						rk=Registry.ClassesRoot;
						break;
					case"HKEY_CURRENT_USER":
						rk=Registry.CurrentUser;
						break;
					case"HKEY_LOCAL_MACHINE":
						rk=Registry.LocalMachine;
						break;
					case"HKEY_USERS":
						rk=Registry.Users;
						break;
					case"HKEY_CURRENT_CONFIG":
						rk=Registry.CurrentConfig;
						break;
					default:
						break;
				}
				//去掉前头的“我的电脑"
				path=path.Substring(6);
				//如果不是根键,找出路径,取得注册键对象
				if(path.IndexOf("\\")>0)
				{
					path=path.Substring(path.IndexOf("\\")+1);
					try
					{
						rk=rk.OpenSubKey(path);
					}
					catch(Exception e)
					{
					//MessageBox.Show(e.Message);
					}
					
				}
			}
			return rk;
		}

		private void AddSubRegistryKey(TreeNode node)
		{
			if(node.Nodes.Count==0)
			{
				//取得节点所代表的键
				RegistryKey rk=GetKeyByNode(node);
				//取得所有子键的名字
				string[] subkeys=rk.GetSubKeyNames();
				//所有子键名加入TreeView
				for(int i=0;i<subkeys.Length;i++)
				{
					node.Nodes.Add(subkeys[i]);
				}
			}
		}


		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.tVRegistryKey = new System.Windows.Forms.TreeView();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.lVRegistryValue = 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.SuspendLayout();
			// 
			// tVRegistryKey
			// 
			this.tVRegistryKey.Dock = System.Windows.Forms.DockStyle.Left;
			this.tVRegistryKey.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tVRegistryKey.ImageList = this.imageList1;
			this.tVRegistryKey.Location = new System.Drawing.Point(0, 0);
			this.tVRegistryKey.Name = "tVRegistryKey";
			this.tVRegistryKey.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
																					  new System.Windows.Forms.TreeNode("我的电脑", 2, 2, new System.Windows.Forms.TreeNode[] {
																																											  new System.Windows.Forms.TreeNode("HKEY_CLASSES_ROOT"),
																																											  new System.Windows.Forms.TreeNode("HKEY_CURRENT_USER"),
																																											  new System.Windows.Forms.TreeNode("HKEY_LOCAL_MACHINE"),
																																											  new System.Windows.Forms.TreeNode("HKEY_USERS"),
																																											  new System.Windows.Forms.TreeNode("HKEY_CURRENT_CONFIG")})});
			this.tVRegistryKey.SelectedImageIndex = 1;
			this.tVRegistryKey.Size = new System.Drawing.Size(232, 461);
			this.tVRegistryKey.TabIndex = 0;
			this.tVRegistryKey.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tVRegistryKey_AfterSelect);
			// 
			// 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;
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(232, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 461);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// lVRegistryValue
			// 
			this.lVRegistryValue.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							  this.columnHeader1,
																							  this.columnHeader2,
																							  this.columnHeader3});
			this.lVRegistryValue.ImeMode = System.Windows.Forms.ImeMode.NoControl;
			this.lVRegistryValue.LargeImageList = this.imageList1;
			this.lVRegistryValue.Location = new System.Drawing.Point(232, 0);
			this.lVRegistryValue.Name = "lVRegistryValue";
			this.lVRegistryValue.Size = new System.Drawing.Size(360, 456);
			this.lVRegistryValue.SmallImageList = this.imageList1;
			this.lVRegistryValue.StateImageList = this.imageList1;
			this.lVRegistryValue.TabIndex = 2;
			this.lVRegistryValue.TabStop = false;
			this.lVRegistryValue.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "名称";
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "类型";
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "数据";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(592, 461);
			this.Controls.Add(this.lVRegistryValue);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.tVRegistryKey);
			this.Name = "Form1";
			this.Text = "注册表浏览器";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void tVRegistryKey_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if(tVRegistryKey.SelectedNode.Text=="我的电脑")
			{//如果选中根结点“我的电脑”,则加入它的所有子节点
				for(int i=0;i<tVRegistryKey.SelectedNode.Nodes.Count;i++)
				{
					try
					{
						AddSubRegistryKey(tVRegistryKey.SelectedNode.Nodes[i]);
					}
					catch(Exception ee)
					{
						MessageBox.Show(ee.Message);
																			
																		
					}
				}

	
				lVRegistryValue.Items.Clear();//清空listview
				//取得节点所代表的键
				RegistryKey rk=GetKeyByNode(tVRegistryKey.SelectedNode);
				string[] Values=rk.GetValueNames();
				string ValueName;
				object ValueType,ValueData;

				for(int i=0;i<Values.Length;i++)
				{
					ValueName=Values[i];
					ValueData=rk.GetValue(ValueName);
					ValueType=ValueData.GetType();
					ListViewItem lvi;

					if( ValueType.ToString()=="System.String")
					{
						ValueType="REG_SZ";
						lvi=new ListViewItem(new String[]{ValueName,ValueType.ToString(),ValueData.ToString()},3);
					}
					else
					{
						ValueType="REG_DWORD";
						lvi=new ListViewItem(new String[]{ValueName,ValueType.ToString(),ValueData.ToString()},4);
					}
					lVRegistryValue.Items.Add(lvi);
				}
			}
		}

		private void tVRegistryKey_BeforeExpand(object sender,System.Windows.Forms.TreeViewCancelEventArgs e)
		{
			for(int i=0;i<tVRegistryKey.SelectedNode.Nodes.Count;i++)
			{
				try
				{
					AddSubRegistryKey(e.Node.Nodes[i]);
				}
				catch(Exception ee)
				{
					MessageBox.Show(ee.Message);
				}
			}
		}

		private void AddRoot()
		{
			for(int i=0;i<tVRegistryKey.TopNode.Nodes.Count;i++)
			{
				try
				{
					AddSubRegistryKey(tVRegistryKey.TopNode.Nodes[i]);
				}
				catch(Exception ee)
				{
					MessageBox.Show(ee.Message);
				}
			}
		}

	

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -