远程注册表读取,和多线程池的应用_c#应用

2008-02-23 05:42:26来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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

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");
}
http://www.cnblogs.com/edobnet/archive/2006/11/24/571383.html


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: c# md5加密算法的实例_c#应用

下一篇: 由c 转向c#:我们需要注意哪些方面的变化_c#应用