国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

使用C#獲取局域網內所有機器

2023-05-12 12:29:40
字體:
來源:轉載
供稿:網友

一直想開發一個局域網機器管理軟件,但獲得局域網內的所有機器卻是很困難,在網上搜索到一個簡單的方法,其原理很簡單,在cmd.exe下面ping一下幾臺機,然后用arp -aming令查看一下,這種方式比開多線程去循環掃描的方式來的簡單而有效。

//首先來個循環ping一下那個網段的主機。

//其次用以下的函數去獲取所有的局域網內有響應的ip地址列表

public static ArrayList GetAllLocalMachines()
{

Process p = new Process();

p.StartInfo.FileName = "cmd.exe";

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("arp -a");

p.StandardInput.WriteLine("exit");

ArrayList list = new ArrayList();

StreamReader reader = p.StandardOutput;

string IPHead = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString().Substring(0, 3);

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

{

           line = line.Trim();

           if (line.StartsWith(IPHead) && (line.IndexOf("dynamic") != -1))
           {

                  string IP = line.Substring(0, 15).Trim();
                  string Mac = line.Substring(line.IndexOf("-") - 2, 0x11).Trim();
                  LocalMachine localMachine = new LocalMachine();
                  localMachine.MachineIP = IP;
                  localMachine.MachineMAC = Mac;
                  localMachine.MachineName = "";
                  list.Add(localMachine);
           }
}
return list;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安阳县| 平原县| 海城市| 额尔古纳市| 灵川县| 长岛县| 上蔡县| 鸡泽县| 黄浦区| 涪陵区| 广河县| 阳新县| 南投县| 吉安县| 瑞安市| 如皋市| 吉水县| 九龙城区| 岳普湖县| 临西县| 云安县| 渭源县| 新邵县| 兴安县| 合川市| 临夏市| 桃园县| 白朗县| 昭苏县| 内江市| 儋州市| 安顺市| 会宁县| 芒康县| 柘城县| 西乌| 富民县| 临沧市| 中西区| 乐都县| 冕宁县|