使用cmd连接wifi(netsh命令)

2018-02-09  本文已影响3742人  哆啦A梦没有毛

Step1.创建配置文件

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>MI 6</name>
    <SSIDConfig>
        <SSID>
            <name>MI 6</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>manual</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>{password}</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
        <enableRandomization>false</enableRandomization>
    </MacRandomization>
</WLANProfile>

name和SSID可以不同(最好设为一致),name是配置文件名称,SSID是要连接的wifi名;
connectionMode可以为手动连接的manual或者自动连接的auto;
keyMaterial处填写密码,无密码状态如下:

<security>
    <authEncryption>
        <authentication>open</authentication>
        <encryption>none</encryption>
        <useOneX>false</useOneX>
    </authEncryption>
</security>

Step2.添加配置文件

比较奇葩的是当有两个SSID相同但name不同的配置时,这两个wifi会同时出现在当前可连接的wifi列表中,而且显示的是name名:



所以应当尽量避免这种情况

Step3.进行连接

netsh wlan connect name="MI 6"
重要的事情说三遍:这里的name是刚刚添加过的配置文件中的name,而不是配置文件名!而不是配置文件名!而不是配置文件名!因为我刚开始就是栽在这里头了,老是连不上。
连接成功的提示信息为已成功完成连接请求。

常用netsh命令总结:

使用命令行加配置文件的方式连接wifi看似没什么卵用,实际用处非常大,比如可以直接在C#中通过此方式取得可连接wifi列表,相比封装的ManagedWifi要简洁得多

using System;  
using System.Diagnostics;  
  
namespace WifiTest  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Process proc = new Process();  
            proc.StartInfo.CreateNoWindow = true;  
            proc.StartInfo.FileName = "netsh";  
            proc.StartInfo.Arguments = "wlan show networks mode=bssid";  
            proc.StartInfo.RedirectStandardOutput = true;  
            proc.StartInfo.UseShellExecute = false;  
            proc.Start();  
            string output = proc.StandardOutput.ReadToEnd();  
            proc.WaitForExit();  
  
            Console.WriteLine(output);  
            Console.Read();  
        }  
    }  
}  

参考资料:

上一篇 下一篇

猜你喜欢

热点阅读