欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 多线程读取注册表,加载至TreeView

程序员文章站 2024-01-21 09:19:46
复制代码 代码如下:using system; using system.drawing; using system.windows.forms; using system...
复制代码 代码如下:

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