Methods for reading MAC addresses by C # programs

Source: Internet
Author: User

Below are several types of C #ProgramRead the MAC address. In this example, the MAC address of all NICS is read. If you only need to read one of them, just modify it.

1. Read the MAC address using the ipconfig command /// <Summary>
/// Obtain the MAC address of the NIC based on the output stream of the command ipconfig/All.
/// </Summary>
/// <Returns> </returns>
Public Static List < String > Getmacbyipconfig ()
{
List < String > Macs = New List < String > (); Processstartinfo startinfo = new processstartinfo ("Ipconfig","/All");
Startinfo. useshellexecute = false;
Startinfo. redirectstandardinput = true;
Startinfo. redirectstandardoutput = true;
Startinfo. redirectstandarderror = true;
Startinfo. createnowindow = true;
PROCESS p = Process. Start (startinfo );
// Intercepting output streams
Streamreader = P. standardoutput;
String Line = Reader. Readline ();

While ( ! Reader. endofstream)
{
If ( ! String . Isnullorempty (line ))
{
Line = Line. Trim ();

If(Line. startswith ("Physical address"))
{
Macs. Add (line );
}
}
Line=Reader. Readline ();
}

// Wait for the program to run and exit the process
P. waitforexit ();
P. Close ();
Reader. Close ();
 
Return MACs;
} 2 read the MAC address Through WMI. 1) This method depends on the System Service of WMI. This service is generally not disabled. However, if the system service is missing or has problems, this method cannot obtain the MAC address. /// <Summary>
/// Use WMI to read the NIC Mac in the system information
/// </Summary>
/// <Returns> </returns>
Public Static List < String > Getmacbywmi ()
{
List < String > Macs = New List < String > ();
Try
{
String Mac = "" ;
Managementclass MC = New Managementclass ( " Win32_networkadapterconfiguration " );
Managementobjectcollection MoC = MC. getinstances ();
Foreach (Managementobject Mo In MOC)
{
If (( Bool ) Mo [ " Ipenabled " ])
{
Mac = Mo [ " Macaddress " ]. Tostring ();
Macs. Add (Mac );
}
}
MoC = Null ;
MC = Null ;
}
Catch
{
}

ReturnMACs;
}3. Read the MAC address through networkinterface. 1) if the current Nic is disabled (the hardware is hard-disabled), the MAC address of the NIC cannot be obtained, (You can disable the NIC for testing ). 2) if multiple NICs are enabled, the first returned address is the information about the recently enabled network connection.//Returns the object that describes the network interfaces on the Local Computer (network interfaces, also known as network adapters ).
PublicStaticNetworkinterface [] netcardinfo ()
{
ReturnNetworkinterface. getallnetworkinterfaces ();
}

/// <Summary>
/// Read Nic MAC through networkinterface
/// </Summary>
/// <Returns> </returns>
Public Static List < String > Getmacbynetworkinterface ()
{
List < String > Macs = New List < String > ();
Networkinterface [] Interfaces = Networkinterface. getallnetworkinterfaces ();
Foreach (Networkinterface Ni In Interfaces)
{
Macs. Add (Ni. getphysicaladdress (). tostring ());
}
Return MACs;
} 4 read the MAC address through sendarp /// <Summary>
/// Obtain the MAC address of the NIC through sendarp
/// This method fails when the network is disabled or not connected to the network (for example, no network cable is inserted ).
/// </Summary>
/// <Param name = "remoteip"> </param>
/// <Returns> </returns>
Public Static String Getmacbysendarp ( String Remoteip)
{
Stringbuilder macaddress = New Stringbuilder ();

Try
{
Int32 remote=Inet_addr (remoteip );

Int64 macinfo=NewInt64 ();
Int32 Length=6;
Sendarp (remote,0,RefMacinfo,RefLength );

StringTemp=Convert. tostring (macinfo,16). Padleft (12,'0'). Toupper ();

Int X = 12 ;
For ( Int I = 0 ; I < 6 ; I ++ )
{
If (I = 5 )
{
Macaddress. append (temp. substring (x - 2 , 2 ));
}
Else
{
Macaddress. append (temp. substring (x - 2 , 2 ) + " - " );
}
X -= 2 ;
}

ReturnMacaddress. tostring ();
}
Catch
{
ReturnMacaddress. tostring ();
}
}

[Dllimport ( " Iphlpapi. dll " )]
Private Static Extern Int Sendarp (int32 DEST, int32 host, Ref Int64 Mac, Ref Int32 length );
[Dllimport ( " Ws2_32.dll " )]
Private Static Extern Int32 inet_addr ( String IP ); 5. Read the MAC address from the Registry

You can obtain the physical NIC address by reading the registry entry Windows Genuine Advantage.

1) if the registry key is modified, the MAC address cannot be obtained.

 

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows Genuine Advantage

 

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.