2015-08-14 85 views
1

我有按類型獲取網絡接口名稱代碼(EX =檢測無線適配器只)按名稱獲取網絡接口不被鍵入c#

private void Form1_Load(object sender, EventArgs e) 
    { 
     /// Detecting Wireless Adaptors Using Linq 

     IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(network => network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && network.Name == "Microsoft Hosted Network Virtual Adapter"); 

     ////Modified by to select only the active wireless adaptor by using below Linq statement 
     ////.Where(network => network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) 

     ////To detect all Ethernet and wireless adaptors you can use below statement 
     ////.Where(network => network.OperationalStatus == OperationalStatus.Up && (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet || network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)) 

     ///Add Items To Drop Down List 
     cmbAdptors.DisplayMember = "Description"; 
     cmbAdptors.ValueMember= "Id"; 
     foreach (NetworkInterface item in nics) 
     { 
      cmbAdptors.Items.Add(item); 
     } 
     if (cmbAdptors.Items.Count > 0) 
      cmbAdptors.SelectedIndex = 0; 

    } 

    private void cmbAdptors_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      if (cmbAdptors.SelectedItem is NetworkInterface) 
      { 
       slectedNic = cmbAdptors.SelectedItem as NetworkInterface; 
       uniCastIPInfo = null; 
       ///Populating IPv4 address 
       if (slectedNic != null && slectedNic.GetIPProperties().UnicastAddresses != null) 
       { 
        UnicastIPAddressInformationCollection ipInfo = slectedNic.GetIPProperties().UnicastAddresses; 

        foreach (UnicastIPAddressInformation item in ipInfo) 
        { 
         if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
         { 
          lblIP.Text = item.Address.ToString(); 
          uniCastIPInfo = item;        
          break; 
         } 
        } 
       } 

       BandwidthCalculator(uniCastIPInfo, slectedNic); 
       wirelssUpdator.Enabled = true; 
      } 

     } 
     catch (Exception ex) 
     {    
      throw; 
     } 
    } 

這裏是代碼

,但我想,它的名字獲得網絡無線適配器

爲EX =如果適配器有名爲「微軟託管的網絡虛擬適配器」

所以,如果我指定,只檢測該適配器,如果可

請幫助

回答

0

這個怎麼樣

IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(network => network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && network.Name == "name you want"); 


foreach(var nIf in nics) 
cmbAdaptors.Items.Add(nIf.Name); 
+0

它不工作。幫幫我 ! – aditya

+0

給了我們更多的代碼,而不僅僅是一條線。嘗試我編輯的答案 –

+0

上面給出的幾乎完整的代碼 – aditya