远程注册表读取,与多线程池的应用.
界面如下:
一般用在域环境下,读取客户机注册的配制信息.
主要代码如下:
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");
}
{
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");
}