سلام بر برنامه نویسان عزیز 😉 در این مطلب از وب سایت پروگرم 98 کد Registry Tree با استفاده از کلاس در سی شارپ را آماده کردم. انشاالله که این کد سی شارپ براتون مفید باشد.
کد Registry Tree با استفاده از کلاس در سی شارپ :
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
public class RegistryTreeClass: TreeView
{
public RegistryTreeClass()
{
ImageList = new ImageList();
ImageList.Images.Add(new Bitmap(GetType(), "CLOSED.BMP"));
ImageList.Images.Add(new Bitmap(GetType(), "OPEN.BMP"));
RootNodes();
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
{
base.OnBeforeExpand(e);
BeginUpdate();
foreach (TreeNode tn in e.Node.Nodes)
{
AddBranch(tn);
}
EndUpdate();
}
public void RootNodes()
{
BeginUpdate();
TreeNode tnHKCR = new TreeNode("HKEY_CLASSES_ROOT",0,1);
Nodes.Add(tnHKCR);
AddBranch(tnHKCR);
TreeNode tnHKCU = new TreeNode("HKEY_CURRENT_USER",0,1);
Nodes.Add(tnHKCU);
AddBranch(tnHKCU);
TreeNode tnHKLM = new TreeNode("HKEY_LOCAL_MACHINE",0,1);
Nodes.Add(tnHKLM);
AddBranch(tnHKLM);
TreeNode tnHKU = new TreeNode("HKEY_USERS",0,1);
Nodes.Add(tnHKU);
AddBranch(tnHKU);
SelectedNode = tnHKLM;
EndUpdate();
}
public void AddBranch(TreeNode tn)
{
if (tn.Nodes.Count > 0) return;
string strRegistryPath = tn.FullPath;
RegistryKey regBranch = null;
if (strRegistryPath.StartsWith("HKEY_CLASSES_ROOT"))
regBranch = Registry.ClassesRoot;
else if (strRegistryPath.StartsWith("HKEY_CURRENT_USER"))
regBranch = Registry.CurrentUser;
else if (strRegistryPath.StartsWith("HKEY_LOCAL_MACHINE"))
regBranch = Registry.LocalMachine;
else if (strRegistryPath.StartsWith("HKEY_USERS"))
regBranch = Registry.Users;
RegistryKey regSubKey = null;
try
{
if (null != tn.Parent)
{
// We need the path minus the top level tree.
int nPosPathSeparator = strRegistryPath.IndexOf(this.PathSeparator);
string strSubkey = strRegistryPath.Substring(nPosPathSeparator+1);
regSubKey = regBranch.OpenSubKey(strSubkey);
}
else
regSubKey = regBranch;
}
catch
{
return;
}
string[] astrSubkeyNames = regSubKey.GetSubKeyNames();
for (int i=0; i < astrSubkeyNames.Length; i++)
{
TreeNode tnBranch = new TreeNode(astrSubkeyNames[i],0,1);
tn.Nodes.Add(tnBranch);
}
}
}
public class MainForm : System.Windows.Forms.Form
{
private RegistryTreeClass rtvRegistry;
public MainForm()
{
InitializeComponent();
this.rtvRegistry = new RegistryTreeClass();
this.SuspendLayout();
this.rtvRegistry.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtvRegistry.ImageIndex = -1;
this.rtvRegistry.Name = "rtvRegistry";
this.rtvRegistry.SelectedImageIndex = -1;
this.rtvRegistry.Size = new System.Drawing.Size(292, 273);
this.rtvRegistry.TabIndex = 0;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rtvRegistry});
this.ResumeLayout(false);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "MainForm";
this.Text = "Registry with class";
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
public class RegistryTreeClass: TreeView
{
public RegistryTreeClass()
{
ImageList = new ImageList();
ImageList.Images.Add(new Bitmap(GetType(), "CLOSED.BMP"));
ImageList.Images.Add(new Bitmap(GetType(), "OPEN.BMP"));
RootNodes();
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
{
base.OnBeforeExpand(e);
BeginUpdate();
foreach (TreeNode tn in e.Node.Nodes)
{
AddBranch(tn);
}
EndUpdate();
}
public void RootNodes()
{
BeginUpdate();
TreeNode tnHKCR = new TreeNode("HKEY_CLASSES_ROOT",0,1);
Nodes.Add(tnHKCR);
AddBranch(tnHKCR);
TreeNode tnHKCU = new TreeNode("HKEY_CURRENT_USER",0,1);
Nodes.Add(tnHKCU);
AddBranch(tnHKCU);
TreeNode tnHKLM = new TreeNode("HKEY_LOCAL_MACHINE",0,1);
Nodes.Add(tnHKLM);
AddBranch(tnHKLM);
TreeNode tnHKU = new TreeNode("HKEY_USERS",0,1);
Nodes.Add(tnHKU);
AddBranch(tnHKU);
SelectedNode = tnHKLM;
EndUpdate();
}
public void AddBranch(TreeNode tn)
{
if (tn.Nodes.Count > 0) return;
string strRegistryPath = tn.FullPath;
RegistryKey regBranch = null;
if (strRegistryPath.StartsWith("HKEY_CLASSES_ROOT"))
regBranch = Registry.ClassesRoot;
else if (strRegistryPath.StartsWith("HKEY_CURRENT_USER"))
regBranch = Registry.CurrentUser;
else if (strRegistryPath.StartsWith("HKEY_LOCAL_MACHINE"))
regBranch = Registry.LocalMachine;
else if (strRegistryPath.StartsWith("HKEY_USERS"))
regBranch = Registry.Users;
RegistryKey regSubKey = null;
try
{
if (null != tn.Parent)
{
// We need the path minus the top level tree.
int nPosPathSeparator = strRegistryPath.IndexOf(this.PathSeparator);
string strSubkey = strRegistryPath.Substring(nPosPathSeparator+1);
regSubKey = regBranch.OpenSubKey(strSubkey);
}
else
regSubKey = regBranch;
}
catch
{
return;
}
string[] astrSubkeyNames = regSubKey.GetSubKeyNames();
for (int i=0; i < astrSubkeyNames.Length; i++)
{
TreeNode tnBranch = new TreeNode(astrSubkeyNames[i],0,1);
tn.Nodes.Add(tnBranch);
}
}
}
public class MainForm : System.Windows.Forms.Form
{
private RegistryTreeClass rtvRegistry;
public MainForm()
{
InitializeComponent();
this.rtvRegistry = new RegistryTreeClass();
this.SuspendLayout();
this.rtvRegistry.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtvRegistry.ImageIndex = -1;
this.rtvRegistry.Name = "rtvRegistry";
this.rtvRegistry.SelectedImageIndex = -1;
this.rtvRegistry.Size = new System.Drawing.Size(292, 273);
this.rtvRegistry.TabIndex = 0;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rtvRegistry});
this.ResumeLayout(false);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "MainForm";
this.Text = "Registry with class";