C\#다 중 스 레 드 에서 레 지 스 트 를 읽 고 TreeView 로 불 러 옵 니 다.


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
namespace
{
public partial class Form1 : Form
{
private delegate void LoadTreeView ( TreeNode node );//
public Form1()
{
InitializeComponent ();
}
private void Form1_Load( object sender , EventArgs e )
{
Thread thread = new Thread (new ThreadStart (ReadAllReg));
thread.Start ();
}
/// <summary>
///
/// </summary>
private void ReadAllReg()
{
LoadTreeView loadTreeView = new LoadTreeView (SendToTreeView);
RegistryKey reg = Registry.ClassesRoot;
TreeNode node = new TreeNode ("HKEY_CLASSES_ROOT");
ReadRegistry (reg , node);
reg.Close ();
if( treeView .InvokeRequired )// TreeView
treeView .BeginInvoke (loadTreeView , node);
else
{
treeView .Nodes.Add (node);
}
/*----------------------------------------------------*/
reg = Registry.CurrentUser;
node = new TreeNode ("HKEY_CURRENT_USER");
ReadRegistry (reg , node);
reg.Close ();
if( treeView .InvokeRequired )// TreeView
treeView .BeginInvoke (loadTreeView , node);
else
{
treeView .Nodes.Add (node);
}
/*----------------------------------------------------*/
reg = Registry.LocalMachine;
node = new TreeNode ("HKEY_LOCAL_MACHINE");
ReadRegistry (reg , node);
reg.Close ();
if( treeView .InvokeRequired )// TreeView
treeView .BeginInvoke (loadTreeView , node);
else
{
treeView .Nodes.Add (node);
}
/*----------------------------------------------------*/
reg = Registry.Users;
node = new TreeNode ("HKEY_USERS");
ReadRegistry (reg , node);
reg.Close ();
if( treeView .InvokeRequired )// TreeView
treeView .BeginInvoke (loadTreeView , node);
else
{
treeView .Nodes.Add (node);
}
/*----------------------------------------------------*/
reg = Registry.CurrentConfig;
node = new TreeNode ("HKEY_CURRENT_CONFIG");
ReadRegistry (reg , node);
reg.Close ();
if( treeView .InvokeRequired )// TreeView
treeView .BeginInvoke (loadTreeView , node);
else
{
treeView .Nodes.Add (node);
}
}
/// <summary>
/// TreeView
/// </summary>
/// <param name="node">TreeNode</param>
private void SendToTreeView( TreeNode node )
{
treeView .Nodes.Add (node);
treeView .Refresh ();
}
/// <summary>
///
/// </summary>
/// <param name="reg"> </param>
/// <param name="nodes"> , </param>
internal void ReadRegistry( RegistryKey reg , TreeNode nodes )//
{
foreach( string names in reg.GetSubKeyNames () )
{
TreeNode node = new TreeNode (names);
try
{
RegistryKey r = reg.OpenSubKey (names , false);
if( r != null )
{
ReadRegistry (r , node);
r.Close ();
}
else
{
return;
}
}
catch( Exception err )
{
continue;
}
nodes.Nodes.Add (node);
}
}
}
}

좋은 웹페이지 즐겨찾기