使用C#连接wifi
需要安装库 Managed Native Wifi 获取Wifi列表 示例代码 private List<AvailableNetworkPack> GetAvaliableNetworks() { var networks = new List<AvailableNetworkPack>(); foreach (var network in NativeWifi.EnumerateAvailableNetworks()) { if (!networks.Contains(network)) { networks.Add(network); } } return networks; } 连接wifi 创建Profile 应该支持这些类型open, WEP and WPA-PSK的网络 private static string CreateSecurityWifiProfile(string ssid, string password) { string hex = CreateHexSSIDName(ssid); return string.Format(@"<?xml version=""1.0""?> <WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1""> <name>{0}</name> <SSIDConfig> <SSID> <hex>{2}</hex> <name>{0}</name> </SSID> </SSIDConfig> <connectionType>ESS</connectionType> <connectionMode>auto</connectionMode> <MSM> <security> <authEncryption> <authentication>WPA2PSK</authentication> <encryption>AES</encryption> <useOneX>false</useOneX> </authEncryption> <sharedKey> <keyType>passPhrase</keyType> <protected>false</protected> <keyMaterial>{1}</keyMaterial> </sharedKey> </security> </MSM> <MacRandomization xmlns=""http://www.microsoft.com/networking/WLAN/profile/v3""> <enableRandomization>false</enableRandomization> </MacRandomization> </WLANProfile>", ssid, password, hex); } private static string CreateOpenWifiProfile(string ssid) { string hex = CreateHexSSIDName(ssid); return string.Format(@"<?xml version=""1.0""?> <WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1""> <name>{0}</name> <SSIDConfig> <SSID> <hex>{1}</hex> <name>{0}</name> </SSID> </SSIDConfig> <connectionType>ESS</connectionType> <connectionMode>auto</connectionMode> <MSM> <security> <authEncryption> <authentication>open</authentication> <encryption>none</encryption> <useOneX>false</useOneX> </authEncryption> </security> </MSM> </WLANProfile>", ssid, hex); } private static string CreateHexSSIDName(string ssid) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(ssid); return BitConverter.ToString(bytes).Replace("-", ""); } 连接到wifi var networks = GetAvaliableNetworks(); var network = networks.First(n => n.Ssid.ToString() == "gophers"); var profile = NativeWifi.EnumerateProfiles().FirstOrDefault(x => x.Name == "NTSP_CD"); if (profile is null) { var profileXml = CreateProfile("gophers", "gopherspwd"); // create a profile var profileResult = NativeWifi.SetProfile(network.Interface.Id, ProfileType.PerUser, profileXml, "AES", true); } // do connect var wasConnected = NativeWifi.ConnectNetwork(network.Interface.Id, network.Ssid.ToString(), network.BssType); 附录 一些常用的profile配置xml 摘自 项目https://github.com/DigiExam/simplewifi/ ...