Wednesday, August 17, 2011

MAC ID Generating In C#.NET

Step 1:

Go to the solution Explorer--> Right Click --> Add Refrence --> System.Management

Step 2:

Add 2 NameSpaces :
1).using System.Management;
2).using System.Management.Instrumentation;

Step 3:

//Create object for management class
ManagementClass mc=newManagementClass("Win32_NetworkAdapterConfiguration");

Step 4:
//management object collection represent all collections from managemnet class                                               
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
 {
if(MACAddress == String.Empty) 
// only return MAC Address from first card            
 {
if ((bool)mo["IPEnabled"] == true)
 MACAddress = mo["MacAddress"].ToString(); 
 }
mo.Dispose();     
}         
//after every 2 digits it returns ":"      
 MACAddress = MACAddress.Replace(":", "");                       MessageBox.Show(MACAddress);

No comments:

Post a Comment