使用ManagedWifi连接到c#中的wifi

使用ManagedWifi连接到c#中的wifi,c#,wifi,C#,Wifi,我正在尝试从我的代码连接到wifi网络。我发现Managedwifi是一个好方法。我用它扫描wifi网络,这很好。但问题是我无法连接到网络,也无法在互联网上找到示例代码。我用谷歌搜索了一下,但没有结果! 请帮我连接到网络! 这是尝试的代码 static void Main(string[] args) { WlanClient client = new WlanClient(); foreach (WlanClient.WlanInterface wlan

我正在尝试从我的代码连接到wifi网络。我发现Managedwifi是一个好方法。我用它扫描wifi网络,这很好。但问题是我无法连接到网络,也无法在互联网上找到示例代码。我用谷歌搜索了一下,但没有结果! 请帮我连接到网络! 这是尝试的代码

static void Main(string[] args)
    {
        WlanClient client = new WlanClient();
        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        {
            // Lists all available networks
            Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
            foreach (Wlan.WlanAvailableNetwork network in networks)
            {
                Console.WriteLine("Found network with SSID {0} || Secured : {1}.", GetStringForSSID(network.dot11Ssid),network.securityEnabled);
            }
            //wlanIface.DeleteProfile("Xperia Arc");
            //string profileName = GetStringForSSID(networks[0].dot11Ssid); // this is also the SSID
            string profileName = "Xperia Arc";
            string mac = "52544131303235572D454137443638";
            string key = "9090090900";
            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>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</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);
            try
            {
                wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
            }
            catch (Exception)
            {
                Console.WriteLine("Error Occured!");
                //throw;
            }
            while (wlanIface.InterfaceState.ToString()=="Associating")
            {
                ;
            }
            Console.WriteLine(wlanIface.InterfaceState.ToString() + wlanIface.);
            //Console.WriteLine(wlanIface.CurrentConnection.profileName +" "+ wlanIface.InterfaceState.ToString());
        }

    }

    static string GetStringForSSID(Wlan.Dot11Ssid ssid)
    {
        return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
    }
static void Main(字符串[]args)
{
WlanClient client=新的WlanClient();
foreach(客户端接口中的WlanClient.WlanInterface-wlanface)
{
//列出所有可用的网络
Wlan.WlanAvailableNetwork[]networks=wlanIface.GetAvailableNetworkList(0);
foreach(Wlan.WLA网络中的可用网络)
{
Console.WriteLine(“找到SSID为{0}| |安全的网络:{1}.”,GetStringForSSID(network.dot11Ssid),network.securityEnabled);
}
//wlanIface.DeleteProfile(“Xperia弧”);
//string profileName=GetStringForSSID(网络[0].dot11Ssid);//这也是SSID
string profileName=“Xperia Arc”;
字符串mac=“52544131303235572D4541374443638”;
字符串键=“9090090900”;
string profileXml=string.Format(“{0}{1}{0}ESSopenWEPfalsenetworkKeyfalse{2}0”,profileName,mac,key);
尝试
{
SetProfile(Wlan.WlanProfileFlags.AllUser,profileXml,true);
wlanIface.Connect(Wlan.WlanConnectionMode.Profile,Wlan.Dot11BssType.Any,profileName);
}
捕获(例外)
{
Console.WriteLine(“发生错误!”);
//投掷;
}
while(wlanIface.InterfaceState.ToString()=“关联”)
{
;
}
Console.WriteLine(wlanIface.InterfaceState.ToString()+wlanIface.);
//Console.WriteLine(wlanIface.CurrentConnection.profileName+“”+wlanIface.InterfaceState.ToString());
}
}
静态字符串GetStringForSSID(Wlan.Dot11Ssid ssid)
{
返回Encoding.ASCII.GetString(ssid.ssid,0,(int)ssid.SSIDLength);
}
有关的一般用法,请参见:

您关于概要xml中的“mac”的问题:它是ssid的十六进制表示形式

您的代码存在多个问题:

  • 不要运行所有接口并连接到同一个wifi网络,只需使用第一个可用接口即可
  • 等待接口状态“关联”的while循环将无法工作,因为接口通常仍处于“断开连接”状态。所以最好等到“连接”或超时
因此,您的代码应该如下所示:

static void Main(string[] args)
    {
        WlanClient client = new WlanClient();
        WlanClient.WlanInterface wlanIface = client.Interfaces.FirstOrDefault();

        if(wlanIface == null)
        {
            Console.WriteLine("No Wifi Interface available!");
            throw new Exception("No Wifi Interface available!");
        }

        // Lists all available networks
        Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
        foreach (Wlan.WlanAvailableNetwork network in networks)
        {

            Console.WriteLine("Found network with SSID {0} || Secured : {1}.", GetStringForSSID(network.dot11Ssid),network.securityEnabled);
        }

        string profileName = "Xperia Arc";
        string ssid = profileName;
        byte[] ssidBytes = Text.Encoding.Default.GetBytes(ssid);
        string ssidHex = BitConverter.ToString(ssidBytes);
        ssidHex = ssidHex.Replace("-", "");
        string key = "9090090900";

        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>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", ssid, ssidHex, key);

        try
        {
            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
        }
        catch (Exception)
        {
            Console.WriteLine("Error Occured!");
            //throw;
        }

        int timeout = 3000;
        while ((wlanIface.InterfaceState.ToString()<>"Connected") && (timeout >= 0))
        {
               System.Threading.Thread.Sleep(500);
               timeout -= 500;
        }

        Console.WriteLine(wlanIface.InterfaceState.ToString() + wlanIface);
        //Console.WriteLine(wlanIface.CurrentConnection.profileName +" "+ wlanIface.InterfaceState.ToString());
    }
static void Main(字符串[]args)
{
WlanClient client=新的WlanClient();
WlanClient.WlanInterface wlanIface=client.Interfaces.FirstOrDefault();
if(wlanIface==null)
{
控制台。WriteLine(“没有可用的Wifi接口!”);
抛出新异常(“没有可用的Wifi接口!”);
}
//列出所有可用的网络
Wlan.WlanAvailableNetwork[]networks=wlanIface.GetAvailableNetworkList(0);
foreach(Wlan.WLA网络中的可用网络)
{
Console.WriteLine(“找到SSID为{0}| |安全的网络:{1}.”,GetStringForSSID(network.dot11Ssid),network.securityEnabled);
}
string profileName=“Xperia Arc”;
字符串ssid=profileName;
字节[]ssidBytes=Text.Encoding.Default.GetBytes(ssid);
字符串ssidHex=位转换器.ToString(ssidBytes);
ssidHex=ssidHex.Replace(“-”,”);
字符串键=“9090090900”;
string profileXml=string.Format(“{0}{1}{0}ESSopenWEPfalsenetworkKeyfalse{2}0”,ssid,ssidHex,key);
尝试
{
SetProfile(Wlan.WlanProfileFlags.AllUser,profileXml,true);
wlanIface.Connect(Wlan.WlanConnectionMode.Profile,Wlan.Dot11BssType.Any,profileName);
}
捕获(例外)
{
Console.WriteLine(“发生错误!”);
//投掷;
}
int超时=3000;
while((wlanIface.InterfaceState.ToString()“已连接”)&&(超时>=0))
{
系统.线程.线程.睡眠(500);
超时-=500;
}
Console.WriteLine(wlanIface.InterfaceState.ToString()+wlanIface);
//Console.WriteLine(wlanIface.CurrentConnection.profileName+“”+wlanIface.InterfaceState.ToString());
}

有人能告诉我什么是mac吗?这能回答你的问题吗?当wifi设备不止一个时,这会导致错误。我可以问一下解决方案吗?请打开一个新问题,其中包含详细的错误描述和您的代码…wlanIface.Connect(Wlan.WlanConnectionMode.Profile,Wlan.Dot11BssType.Any,ssid,WlanConnectionFlags.AdhocJoinOnly);