?? form1.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 + -