The following is a collection of several C # programs to read the MAC address of the method, the example is to read all the network card MAC address, if only need to read one, slightly modified. 1 read MAC address via ipconfig command///<summary>
///Gets the NIC Mac based on the output stream of the Intercept ipconfig/all command
///</summary>
///<returns ></returns>
public static List<string> Getmacbyipconfig ()
{
list< Span style= "color: #000000;" ><string> Macs Span style= "color: #000000;" >=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);
//Intercept output stream
StreamReader Reader=P.standardoutput;
StringLine=Reader. ReadLine ();
While (! Reader. Endofstream)
{
if! string {
line = line. Trim ();
if line. StartsWith ( "physical Address " {
macs. ADD (line);
}
}
Line = reader. ReadLine ();
}
wait for the program to finish executing the exit process
p.WaitForExit ();
P.close ();
Reader. Close ();
return Macs;
}2 read MAC address through WMI 1) This method relies on WMI's system services, which are generally not shut down, but cannot get the MAC address if the system service is missing or if there is a problem.///<summary>
///Read the network card MAC in the System information via WMI
///</summary>
///<returns></returns>
PublicStaticList<String>GETMACBYWMI ()
{
List<String>Macs=NewList<String>();
Try
{
StringMac="";
ManagementClass MC=NewManagementClass ("Win32_NetworkAdapterConfiguration");
Managementobjectcollection MOC=Mc. GetInstances ();
Foreach(ManagementObject moInchMoC
{
If((bool) mo["IPEnabled"])
{
Mac=mo[ "macaddress" ]. ToString ();
Macs. ADD (Mac
}
moc = null mc =null }
catch
{
}
Return Macs;
} 3 Read MAC address via NetworkInterface 1) If the current network adapter is disabled (hardware is in a hard-off state), the MAC address of the network card cannot be obtained (you can experiment by disabling the network card). 2) If more than one network adapter is currently enabled, the first address returned is information about the most recently enabled network connection//Returns an object that describes the network interface on the local computer (a network interface is also known as a network adapter).
PublicStaticNetworkinterface[] Netcardinfo ()
{
ReturnNetworkinterface.getallnetworkinterfaces ();
}
///<summary>
///Read the Nic Mac via NetworkInterface
///</summary>
///<returns></returns>
PublicStaticList<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 MAC address via Sendarp///<summary>
///Get the Nic Mac from Sendarp
///This method fails when the network is disabled or not connected to the network (for example, a cable is not plugged in)
///</summary>
///<param name= "Remoteip" ></param>
///<returns></returns>
PublicStaticStringGetmacbysendarp (StringREMOTEIP)
{
StringBuilder macAddress=NewStringBuilder ();
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 ();
IntX=12;
For(IntI=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")]
Privatestaticexternint sendarp (Int32 dest, Int32 host, ref Int64 mac, ref Int32 length) ;
[DllImport ("ws2_32.dll")]
privatestaticextern Int32 inet_addr (string IP); 5 read MAC address from registry
A regular user can obtain a physical NIC address by reading the registry key for Windows Genuine advantage.
1) If the registry key is modified, the MAC address cannot be obtained
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Genuine Advantage
Reprint: http://www.cnblogs.com/diulela/archive/2012/04/07/2436111.html
Methods for reading MAC addresses by several C # programs