c#连接wifi

.Net技术 码拜 9年前 (2015-05-11) 3090次浏览 0个评论
 class WIFISSID
    {
        public string SSID = "NONE";
        public string dot11DefaultAuthAlgorithm = "";
        public string dot11DefaultCipherAlgorithm = "";
        public bool networkConnectable = true;
        public string wlanNotConnectableReason = "";
        public int wlanSignalQuality = 0;
        public WlanClient.WlanInterface wlanInterface = null;
    }

  public void ConnectToSSID(WIFISSID ssid)
        {
            // Connects to a known network with WEP security
            string profileName = ssid.SSID; // this is also the SSID
            string mac = StringToHex(profileName); //
            string key = "166397954";
            //string profileXml = 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>New{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, key);
            string profileXml = string.Format("<?xml version="1.0"?> <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig> <connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>WPAPSK</authentication><encryption>TKIP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>", profileName, key);
            //string profileXml2 = "<?xml version="1.0"?><WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"><name>Hacker SSID</name><SSIDConfig><SSID><hex>54502D4C494E4B5F506F636B657441505F433844323632</hex><name>TP-LINK_PocketAP_C8D262</name></SSID>        </SSIDConfig>        <connectionType>ESS</connectionType><connectionMode>manual</connectionMode><MSM> <security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>";
            ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
            ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
            //string myProfileXML = 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>manual</connectionMode><MSM><security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", profileName, mac);
            //ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, myProfileXML, true);
            //ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
            //Console.ReadKey();
        }

我在运行这段代码时他会进行连接wifi但是没有反映。不会连上,也不会有什么提示。而且有时候会创建一个“wifi名字“2这样的新WiFi。求大神帮忙。。。

没有人么。我试过没密码的可以连接。感觉可能是profileXml的问题。但是我修改后还是不能实现。
5分
http://i.isclab.org/?p=299  去看看吧  不知道是不是你想要的
我基本上就是照着他写的。它是连接的无密码wifi我想让他能连有密码wifi。改了一下profileXml但是不行。
10分
不同的网络用不同的格式
http://msdn.microsoft.com/en-us/library/windows/desktop/aa369853%28v=vs.85%29.aspx

  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

Quote: 引用 4 楼 feiyun0112 的回复:

不同的网络用不同的格式
http://msdn.microsoft.com/en-us/library/windows/desktop/aa369853%28v=vs.85%29.aspx
恩恩。感谢回复啊。这个我知道。我也修改了。但是还是不行,就显示正在连接但是就没有结果了。也不会有任何提示,而且下次手动连wifi必须忘记密码后才能在连上。而且我发现通过程序检测到的认证方式和加密方式与我在电脑里看到的不一样c#连接wific#连接wifi这又是为什么呢?

求大神帮忙啊。没有人可以提供点思路么?
学习了,用C++ 比较好点吧
引用 7 楼 yanasdf789 的回复:

学习了,用C++ 比较好点吧

恩恩,看到很多都是c++的,不过我想c#既然提供了这样的方法就应该可以才对,一定是我哪里出错了。但是我发现不了。其实我是无聊,宿舍wifi不稳定,老断开需要重连。我就想写一个断网重连的程序。结果。。。

人工置顶。求奇迹。
有没有解决,遇到相同问题
5分
<keyType>passPhrase</keyType>
改为networkKey看看
换了一个路由器就好了!我个人感觉是网络身份验证模式(wpa-psk或wpa2-psk)和加密方式的问题。我现在的路由器是wpa-psk.加密方式是TKIP+AES!
留个联系方式啊,我最近也在搞wifi连接遇到了问题
+1  不知道楼主解决没 求解决方案
++求解决方案
楼主搞定了没有啊?最近在学如何自动连接/断开wifi。。。希望指点一下!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c#连接wifi
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!