using Microsoft.Win32;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace regViewer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ColumnHeader cHeader1;
		private System.Windows.Forms.ColumnHeader cHeader2;
		private System.Windows.Forms.ColumnHeader cHeader3;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.TreeView tVRegistryKey;
		private System.Windows.Forms.ListView lVRegistryValue;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			InitializeComponent();
				
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </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.splitter1 = new System.Windows.Forms.Splitter();
			this.lVRegistryValue = new System.Windows.Forms.ListView();
			this.cHeader1 = new System.Windows.Forms.ColumnHeader();
			this.cHeader2 = new System.Windows.Forms.ColumnHeader();
			this.cHeader3 = new System.Windows.Forms.ColumnHeader();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			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.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", 0, 1),
																																											  new System.Windows.Forms.TreeNode("HKEY_CURRENT_USER", 0, 1),
																																											  new System.Windows.Forms.TreeNode("HKEY_LOCAL_MACHINE", 0, 1),
																																											  new System.Windows.Forms.TreeNode("HKEY_USERS", 0, 1),
																																											  new System.Windows.Forms.TreeNode("HKEY_CURRENT_CONFIG", 0, 1)})});
			this.tVRegistryKey.SelectedImageIndex = 1;
			this.tVRegistryKey.Size = new System.Drawing.Size(160, 273);
			this.tVRegistryKey.TabIndex = 0;
			this.tVRegistryKey.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tVRegistryKey_AfterSelect);
			this.tVRegistryKey.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tVRegistryKey_BeforeExpand);
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(160, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 273);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// lVRegistryValue
			// 
			this.lVRegistryValue.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							  this.cHeader1,
																							  this.cHeader2,
																							  this.cHeader3});
			this.lVRegistryValue.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lVRegistryValue.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lVRegistryValue.FullRowSelect = true;
			this.lVRegistryValue.Location = new System.Drawing.Point(163, 0);
			this.lVRegistryValue.Name = "lVRegistryValue";
			this.lVRegistryValue.Size = new System.Drawing.Size(245, 273);
			this.lVRegistryValue.SmallImageList = this.imageList1;
			this.lVRegistryValue.TabIndex = 2;
			this.lVRegistryValue.View = System.Windows.Forms.View.Details;
			// 
			// cHeader1
			// 
			this.cHeader1.Text = "名称";
			this.cHeader1.Width = 88;
			// 
			// cHeader2
			// 
			this.cHeader2.Text = "类型";
			this.cHeader2.Width = 87;
			// 
			// cHeader3
			// 
			this.cHeader3.Text = "数据";
			// 
			// imageList1
			// 
			this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			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;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(408, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lVRegistryValue,
																		  this.splitter1,
																		  this.tVRegistryKey});
			this.Name = "Form1";
			this.Text = "注册表浏览器";
			this.ResumeLayout(false);

		}
		#endregion

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

		/// <summary>
		/// The main entry point for the application.
		/// </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();
			//取得节点所代表的键
			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);
				}
			}
		}
	}
}
