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; }
最新评论