准备工作:
下载个可以开启电脑wifi的工具,笔者使用的是360wifi精灵,不得不说,人家做的确实好,我自己写个开启热点的,需要测试下网卡,如果网卡驱动不对还需要回滚到合适版本,有点繁琐,人家大公司的就不需要,毕竟人家自己就做检测电脑驱动的东西,所以为了方便 直接用这种好使的就可以,毕竟重点并不是为了开启wifi,而是获取连接热点的设备 信息。
用到DOS命令
netsh wlan show hosted
这条命令是获取承载网络信息的,就是热点相关信息。
arp -a
这条是获取网址的,我才开始一直把重点放在了netsh wlan命令上面,只能得到mac信息,获取不到ip信息,后来同事做前端说可以通过这条命令获取ip,就试了下,还真可以。
代码
string strInput = "netsh wlan show hosted";
Process hostesPro = new Process();
hostesPro.StartInfo.FileName = "cmd.exe";
hostesPro.StartInfo.UseShellExecute = false;
hostesPro.StartInfo.RedirectStandardInput = true;
hostesPro.StartInfo.RedirectStandardOutput = true;
hostesPro.StartInfo.RedirectStandardError = true;
hostesPro.StartInfo.CreateNoWindow = true;
hostesPro.Start();
hostesPro.StandardInput.WriteLine(strInput + "&exit");
hostesPro.StandardInput.AutoFlush = true;
StreamReader rder = hostesPro.StandardOutput;//截取输出流
List<string> ls = new List<string>();
string hostedStr;
while (!rder.EndOfStream)
{
hostedStr = rder.ReadLine();
ls.Add(hostedStr);
}
string[] arr = Regex.Split(ls[20], "\\s+", RegexOptions.IgnoreCase);
string valMac = arr[1].Replace(":","-");
hostesPro.WaitForExit();
hostesPro.Close();
string str = "arp -a";
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(str + "&exit");
p.StandardInput.AutoFlush = true;
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
List<string> lines = new List<string>();
lines.Add(line);
while (!reader.EndOfStream)
{
line = reader.ReadLine();
lines.Add(line);
}
string ipStr="";
foreach (string s in lines)
{
if (s.Contains(valMac))
{
ipStr = s;
break;
}
}
string[] arrIp = Regex.Split(ipStr, "\\s+", RegexOptions.IgnoreCase);
p.WaitForExit();
p.Close();
Console.ReadKey();