注册
 找回密码
 注册
江西广告网
查看: 821|回复: 1
打印 上一主题 下一主题

远程注册表读取,与多线程池的应用

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2008-12-16 10:09:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
一般用在域环境下,读取客户机注册的配制信息.
主要代码如下:

private void button1_Click(object sender, System.EventArgs e)
{
tabControl1.SelectedIndex=1;

ThreadStart threadDelegate = new ThreadStart(this.doIt);
Thread newThread = new Thread(threadDelegate);
newThread.Start();


}
private int number = 0;
private void doIt()
{
int maxthread = int.Parse(txt_numThread.Text);
string remoteName;
progressBar1.Value = 0;
progressBar1.Maximum =(int)n_ip2.Value-(int)n_ip1.Value;
for(Decimal i = n_ip1.Value;i<=n_ip2.Value;i )
{
remoteName = txt_ipStart.Text "." i.ToString();

WaitCallback async = new WaitCallback(RunGet);
while(true)
{

Monitor.Enter(number);
if(number>5)
Thread.Sleep(1000);
else
break;
Monitor.Exit(number);
}
if(ThreadPool.QueueUserWorkItem(async,remoteName))
Interlocked.Increment(ref number);

//RunGet(remoteName);
}
while(true)
{
//myResetEvent.Set();
Monitor.Enter(number);
if( number <= 0)
break;
else
Thread.Sleep(1000);
Monitor.Exit(number);
}
MessageBox.Show("完成");


}

private void RunGet(object ip)
{
try
{
RegistryKey environmentKey = null;
string remoteName = ip.ToString();
try
{
// Open HKEY_CURRENT_USER\Environment
// on a remote computer.
environmentKey = RegistryKey.OpenRemoteBaseKey(
RegistryHive.LocalMachine, remoteName).OpenSubKey(
txt_key.Text);
}
catch(IOException ex)
{
addMsg(string.Format("{0}: {1}", remoteName, ex.Message));

}

// Print the values.
// MessageBox.Show(string.Format("\nThere are {0} values for {1}.",
// environmentKey.ValueCount.ToString(),
// environmentKey.Name));
//addMsg(environmentKey.GetValue("AutoAdminLogon").ToString());
object result = environmentKey.GetValue(txt_value.Text);
if(result != null)
{
addMsg(string.Format("{0}\t\t{1}",remoteName,result));
addSuccessMsg(remoteName,result.ToString());
}
else
{
addMsg(string.Format("{0}\t\t读取失败",remoteName));
}
// foreach(string valueName in environmentKey.GetValueNames())
// {
// addMsg(string.Format("{0,-20}: {1}", valueName, environmentKey.GetValue(valueName).ToString()));
// }

// Close the registry key.
environmentKey.Close();
}
catch
{
}
progressBar1.PerformStep();
Interlocked.Decrement(ref number);

}
private void addMsg(string msg)
{
lb_msg.Items.Insert(0,msg);
}
private void addSuccessMsg(string Ip,string value)
{
txt_result.Text = string.Format("{0}\t\t{1}\r\n",Ip,value) txt_result.Text;
}

private void button2_Click(object sender, System.EventArgs e)
{
lb_msg.Items.Clear();
}

private void button3_Click(object sender, System.EventArgs e)
{
System.IO.StreamWriter sw = new StreamWriter("C:\\1.txt",false,System.Text.Encoding.Default);
sw.Write(txt_result.Text);
sw.Close();
MessageBox.Show("保存到C:\\1.txt");
}

<
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表