Visual C # How to differentiate physical, virtual, and wireless network interfaces

Source: Internet
Author: User

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. net; <br/> using system. net. networkinformation; <br/> using system. net. sockets; <br/> using Microsoft. win32; <br/> namespace consoledemo <br/>{< br/> /// <summary> <br/> // Title: distinguish between local, virtual, and wireless network cards <br/> // Author: X. x.y <br/> // Date: 2009-08-03 <br/> // description: test Environment vs2008 + XP <br/> /// </Summary> <br/> class Program <br />{< Br/> static void main (string [] ARGs) <br/>{< br/> shownetworkinterfacemessage (); <br/>}< br/> /// <summary> <br/> // display the details of each Nic on the local machine <br/> /// </Summary> <br/> Public static void shownetworkinterfacemessage () <br/> {<br/> networkinterface [] fnetworkinterfaces = networkinterface. getallnetworkinterfaces (); <br/> foreach (networkinterface adapter in fnetworkinterfaces) <br/>{< br/> # region" Nic type "<br/> string fcardtype =" unknown nic "; <br/> string fregistrykey = "system // CurrentControlSet // control // network // {4d36e972-e325-11ce-bfc1-08002be10318} //" + adapter. ID + "// connection"; <br/> registrykey rk = registry. localmachine. opensubkey (fregistrykey, false); <br/> If (rk! = NULL) <br/>{< br/> // differentiate pnpinstanceid <br/> // It is common If PCI is the real network card of the Local Machine. <br/> // mediasubtype is 01 nic, 02 is a wireless network card. <Br/> string fpnpinstanceid = rk. getvalue ("pnpinstanceid ",""). tostring (); <br/> int fmediasubtype = convert. toint32 (rk. getvalue ("mediasubtype", 0); <br/> If (fpnpinstanceid. length> 3 & <br/> fpnpinstanceid. substring (0, 3) = "PCI") <br/> fcardtype = "physical nic"; <br/> else if (fmediasubtype = 1) <br/> fcardtype = "virtual nic"; <br/> else if (fmediasubtype = 2) <br/> fcardtype = "wireless nic "; <br/>}< br/> # E Ndregion <br/> # region "Nic information" <br/> console. writeline ("-----------------------------------------------------------"); <br/> console. writeline ("--" + fcardtype); <br/> console. writeline ("-----------------------------------------------------------"); <br/> console. writeline ("ID ..................: {0} ", adapter. ID); // obtain the network adapter identifier <br/> console. writeline ("name ................: {0} ", adapter. na Me); // obtain the name of the network adapter <br/> console. writeline ("description .........: {0} ", adapter. description); // obtain the interface description <br/> console. writeline ("interface type ......: {0} ", adapter. networkinterfacetype); // obtain the interface type <br/> console. writeline ("is receive only ......: {0} ", adapter. isreceiveonly); // obtain the Boolean value, which indicates whether the network interface is set to receive only data packets. <Br/> console. writeline ("Multicast ............: {0} ", adapter. supportsmulticast); // gets a Boolean value that indicates whether to enable a network interface to receive multicast packets. <Br/> console. writeline ("speed ...............: {0} ", adapter. speed); // speed of the network interface <br/> console. writeline ("physical address ....: {0} ", adapter. getphysicaladdress (). tostring (); // MAC address <br/> ipinterfaceproperties fipinterfaceproperties = adapter. getipproperties (); <br/> unicastipaddressinformationcollection = fipinterfaceproperties. unicastaddresses; <br/> foreach (unicastipaddressinformation in unicastipaddressinformationcollection) <br/>{< br/> If (unicastipaddressinformation. address. addressfamily = addressfamily. interNetwork) <br/> console. writeline ("IP address ..........: {0} ", unicastipaddressinformation. address); // ip address <br/>}< br/> console. writeline (); <br/># endregion <br/>}< br/> console. readkey (); <br/>}< br/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.