Chinaunix首页 | 论坛 | 博客
  • 博客访问: 609631
  • 博文数量: 765
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 18:49
文章分类

全部博文(765)

文章存档

2011年(1)

2008年(764)

我的朋友

分类:

2008-10-16 18:50:04

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

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

【责编:Yoyo】

--------------------next---------------------

阅读(280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~