災爺  

     /// <summary> 
        /// 获取当前域的计算机列表 
        /// </summary> 
        /// <returns></returns> 
        public  List<LocalMachine> GetAllLocalMachines()
        {
            //局域网计算机列表 
           
            List<LocalMachine> machineList = new List<LocalMachine>();


            Process p = new Process();

            p.StartInfo.FileName = "net";

            p.StartInfo.Arguments = "view";

            p.StartInfo.UseShellExecute = false;

            p.StartInfo.RedirectStandardInput = true;

            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.RedirectStandardError = true;

            p.StartInfo.CreateNoWindow = true;

            p.Start();

            p.StandardInput.WriteLine("exit");

            StreamReader reader = p.StandardOutput;

            for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
            {
                line = line.Trim();

                if (line.StartsWith(@"\\"))
                {
                    string name = line.Substring(2).Trim();

                    //如果有路由器,会列出路由器,但是获取不到IP地址,会报错 

                    try
                    {
                        LocalMachine localMachine = new LocalMachine();

                        localMachine.MachineIP  = Dns.GetHostEntry(name).AddressList[0].ToString();

                        localMachine.MachineName  = name;

                        machineList.Add(localMachine);
                    }
                    catch
                    {
                        //MessageBox.Show("Error!");
                       
                    }

                }

            }

            return machineList;
        }

posted on 2011-06-30 18:00  災爺  阅读(741)  评论(0)    收藏  举报