远程注册表读取,与多线程池的应用
<SPAN class=t18>一般用在域环境下,读取客户机注册的配制信息.<BR>主要代码如下:<P>private void button1_Click(object sender, System.EventArgs e)<BR> {<BR> tabControl1.SelectedIndex=1;</P>
<P> ThreadStart threadDelegate = new ThreadStart(this.doIt);<BR> Thread newThread = new Thread(threadDelegate);<BR> newThread.Start();</P>
<P><BR> }<BR> private int number = 0;<BR> private void doIt()<BR> {<BR> int maxthread = int.Parse(txt_numThread.Text);<BR> string remoteName;<BR> progressBar1.Value = 0;<BR> progressBar1.Maximum =(int)n_ip2.Value-(int)n_ip1.Value;<BR> for(Decimal i = n_ip1.Value;i<=n_ip2.Value;i)<BR> {<BR> remoteName = txt_ipStart.Text "." i.ToString();</P>
<P> WaitCallback async = new WaitCallback(RunGet);<BR> while(true)<BR> {<BR> <BR> Monitor.Enter(number);<BR> if(number>5)<BR> Thread.Sleep(1000);<BR> else<BR> break;<BR> Monitor.Exit(number);<BR> }<BR> if(ThreadPool.QueueUserWorkItem(async,remoteName))<BR> Interlocked.Increment(ref number);</P>
<P> //RunGet(remoteName);<BR> }<BR> while(true)<BR> {<BR> //myResetEvent.Set();<BR> Monitor.Enter(number);<BR> if( number <= 0)<BR> break;<BR> else<BR> Thread.Sleep(1000);<BR> Monitor.Exit(number);<BR> }<BR> MessageBox.Show("完成");</P>
<P> <BR> }</P>
<P> private void RunGet(object ip)<BR> {<BR> try<BR> {<BR> RegistryKey environmentKey = null;<BR> string remoteName = ip.ToString();<BR> try<BR> {<BR> // Open HKEY_CURRENT_USER\Environment <BR> // on a remote computer.<BR> environmentKey = RegistryKey.OpenRemoteBaseKey(<BR> RegistryHive.LocalMachine, remoteName).OpenSubKey(<BR> txt_key.Text);<BR> }<BR> catch(IOException ex)<BR> {<BR> addMsg(string.Format("{0}: {1}", remoteName, ex.Message));<BR> <BR> }</P>
<P> // Print the values.<BR> // MessageBox.Show(string.Format("\nThere are {0} values for {1}.", <BR> // environmentKey.ValueCount.ToString(), <BR> // environmentKey.Name));<BR> //addMsg(environmentKey.GetValue("AutoAdminLogon").ToString());<BR> object result = environmentKey.GetValue(txt_value.Text);<BR> if(result != null)<BR> {<BR> addMsg(string.Format("{0}\t\t{1}",remoteName,result));<BR> addSuccessMsg(remoteName,result.ToString());<BR> }<BR> else<BR> {<BR> addMsg(string.Format("{0}\t\t读取失败",remoteName));<BR> }<BR> // foreach(string valueName in environmentKey.GetValueNames())<BR> // {<BR> // addMsg(string.Format("{0,-20}: {1}", valueName, environmentKey.GetValue(valueName).ToString()));<BR> // }</P>
<P> // Close the registry key.<BR> environmentKey.Close();<BR> }<BR> catch<BR> {<BR> }<BR> progressBar1.PerformStep();<BR> Interlocked.Decrement(ref number);</P>
<P> }<BR> private void addMsg(string msg)<BR> {<BR> lb_msg.Items.Insert(0,msg);<BR> }<BR> private void addSuccessMsg(string Ip,string value)<BR> {<BR> txt_result.Text = string.Format("{0}\t\t{1}\r\n",Ip,value) txt_result.Text;<BR> }</P>
<P> private void button2_Click(object sender, System.EventArgs e)<BR> {<BR> lb_msg.Items.Clear();<BR> }</P>
<P> private void button3_Click(object sender, System.EventArgs e)<BR> {<BR> System.IO.StreamWriter sw = new StreamWriter("C:\\1.txt",false,System.Text.Encoding.Default);<BR> sw.Write(txt_result.Text);<BR> sw.Close();<BR> MessageBox.Show("保存到C:\\1.txt");<BR> } </P></SPAN> <
页:
[1]