locked
Get MAC address of client machine using C#

    Question

  • How to get MAC address of client machine using C#? Not looking for remote machine MAC Address.
    Tuesday, October 31, 2006 6:02 AM

Answers

  • to get the MAC Address of the client machine, you can get this using the link and answer I had provided and just comment out the scope options. Those are required really if you are connecting to a remote computer as it requires authentication. So this does work for the local computer:




    ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");

    StringBuilder theQueryBuilder = new StringBuilder();
    theQueryBuilder.Append(
    "SELECT MACAddress FROM Win32_NetworkAdapter");
    ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());
    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
    ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

    foreach (ManagementObject theCurrentObject in theCollectionOfResults)
    {

    string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();
    MessageBox.Show(macAdd);
    }


     

    Tuesday, October 31, 2006 12:32 PM
  • Hi Padam,

         The following hyperlink possesses a c # code snippet that reveals all details pertaining to your network interfaces on the local machine only along with it's description. Ideally if you have installed a single nic on your machine, you should get only one mac address as an output for this function, since mac addresses are associated only with nics & other hardware devices that communicate on a network.

    http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

    If you are going to use this code snippet in your program, then you need to put an if -- else condition checking for the length of macaddress variable  if it's zero then it means it's indicating the mac address for Loopback, if it is a valid value; then that's your network adapter's mac address.

    I hope this answers your question.

    Thursday, November 02, 2006 11:50 AM

All replies

  • Tuesday, October 31, 2006 6:09 AM
  • It is asking following input parameters.

    :- sDomain, sUserName, sPwd. As per my requirement, I can not provide following parameters. As per my requirement, without passing any input parameters, i need MAC address of client macine using C#.

    ConnectionOptions options = new ConnectionOptions();
    options.Username = sDomain + "\\" + sUserName; //could be in domain\user format
    options.Password = sPwd;

    Thanks in advance for your help.

    Tuesday, October 31, 2006 7:02 AM
  • to get the MAC Address of the client machine, you can get this using the link and answer I had provided and just comment out the scope options. Those are required really if you are connecting to a remote computer as it requires authentication. So this does work for the local computer:




    ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");

    StringBuilder theQueryBuilder = new StringBuilder();
    theQueryBuilder.Append(
    "SELECT MACAddress FROM Win32_NetworkAdapter");
    ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());
    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
    ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

    foreach (ManagementObject theCurrentObject in theCollectionOfResults)
    {

    string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();
    MessageBox.Show(macAdd);
    }


     

    Tuesday, October 31, 2006 12:32 PM
  • In this code, I have to hardcode ComputerName, root and what does it mean cimv2.  I have to run this code on unknown clients and not in position to hard code any thing.

    As per my requirement, If somebody execute this code in client machine then it should return MAC address without any input parameters.

    Kindly let me know your comments.

    ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");

    Wednesday, November 01, 2006 4:34 AM
  •  PadamPadam wrote:

    In this code, I have to hardcode ComputerName, root and what does it mean cimv2.  I have to run this code on unknown clients and not in position to hard code any thing.

    As per my requirement, If somebody execute this code in client machine then it should return MAC address without any input parameters.

    Kindly let me know your comments.

    ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");

    I let you know how to include the Computer name which is running the code!

    ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");

    Civm2 is Common Information Model Repositry ( a namespace in WMI to categorize different classes in WMI) More and Very good insight explaination of CIVM2 can be found here:

    http://www.awprofessional.com/articles/article.asp?p=30482&seqNum=5&rl=1

    Best Regards,


     

    Wednesday, November 01, 2006 6:29 AM
  • Thanks for your great help.

    It is throwing a NullReferenceException after executing a code of thread.

    Wednesday, November 01, 2006 9:05 AM
  • I am having a Windows XP OS with NIC card.
    Wednesday, November 01, 2006 9:06 AM
  •  PadamPadam wrote:

    Thanks for your great help.

    It is throwing a NullReferenceException after executing a code of thread.

    Was your code running with hardcoded computer name but with not the change I made?

    I think this is not the situation, Try to elaborate your problem that where actually you getting this error which object has nul reference?

    Best Regards,

    Wednesday, November 01, 2006 10:16 AM
  • No. It is not running under hardcoded computer name.

    It is throwing NullReferenceException error in line: string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();

    <quote>

    ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");

    StringBuilder theQueryBuilder = new StringBuilder();

    theQueryBuilder.Append("SELECT MACAddress FROM Win32_NetworkAdapter");

    ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());

    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);

    ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

    foreach (ManagementObject theCurrentObject in theCollectionOfResults)

    {

    string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();

    MessageBox.Show(macAdd);

    }

    </quote>

    Wednesday, November 01, 2006 10:56 AM
  • Below mentioned code gets executed but it returns 4-5 MAC addresses even passing my system name.

    <quote>

    ManagementObjectCollection queryCollection = null;

    ManagementScope theScope = null;

    theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");

    try

    {

    //query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");

    StringBuilder theQueryBuilder = new StringBuilder();

    theQueryBuilder.Append("SELECT * FROM Win32_NetworkAdapter");

    //"SELECT * FROM Win32_NetworkAdapterConfiguration"

    //"SELECT MACAddress FROM Win32_NetworkAdapter"

    ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(theScope, theQuery);

    queryCollection = searcher.Get();

    //queryCollection = query.Get();

    foreach (ManagementObject mo in queryCollection)

    {

    if (mo["MacAddress"] != null)

    {

    MessageBox.Show(mo["MacAddress"].ToString());

    }

    }

    }

    catch (Exception ex)

    {

    Console.WriteLine(ex.Source);

    Console.WriteLine(ex.Message);

    }

    </quote>

    Wednesday, November 01, 2006 11:20 AM
  • This error gets solved after putting if condition (as per below code) but it is returning 4-5 MAC addresses. Even I am passing system name as an argument.

    I am looking for a one MAC address not multiple. Kindly guide me.

    <quote>

    if (theCurrentObject["MACAddress"] != null )

    {

    string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();

    MessageBox.Show(macAdd);

    }

    </quote>

    Complete code -

    <quote>

    ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");

    StringBuilder theQueryBuilder = new StringBuilder();

    theQueryBuilder.Append("SELECT * FROM Win32_NetworkAdapter");

    ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());

    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);

    ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

    foreach (ManagementObject theCurrentObject in theCollectionOfResults)

    {

    if (theCurrentObject["MACAddress"] != null )

    {

    string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();

    MessageBox.Show(macAdd);

    }

    }

    </quote>

    Wednesday, November 01, 2006 11:31 AM
  • its because:

  • you may have a few network connection devices on the system, like a loopback adapter etc...

  • its using a foreach loop and gets all the MACAddresses. you can just get the 1 you like however there is no set 1 specific MAC Address since you will have a few perhaps, depending on how many network connections you have on the system installed

Wednesday, November 01, 2006 7:04 PM
  • Ok. Thanks for your prompt replies.

    How to get primary MAC address of client system? Kindly do guide me.

    Thursday, November 02, 2006 5:48 AM
  • Hi Padam,

         The following hyperlink possesses a c # code snippet that reveals all details pertaining to your network interfaces on the local machine only along with it's description. Ideally if you have installed a single nic on your machine, you should get only one mac address as an output for this function, since mac addresses are associated only with nics & other hardware devices that communicate on a network.

    http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

    If you are going to use this code snippet in your program, then you need to put an if -- else condition checking for the length of macaddress variable  if it's zero then it means it's indicating the mac address for Loopback, if it is a valid value; then that's your network adapter's mac address.

    I hope this answers your question.

    Thursday, November 02, 2006 11:50 AM
  • Thanks  KarthikNarasimhan. That's what i m looking for.

    <quote>

    using System.Net.NetworkInformation;

    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

    Console.WriteLine("Interface information for {0}.{1} ",

    computerProperties.HostName, computerProperties.DomainName);

    if (nics == null || nics.Length < 1)

    {

    Console.WriteLine(" No network interfaces found.");

    return;

    }

    Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length);

    foreach (NetworkInterface adapter in nics)

    {

    IPInterfaceProperties properties = adapter.GetIPProperties(); // .GetIPInterfaceProperties();

    PhysicalAddress address = adapter.GetPhysicalAddress();

    MessageBox.Show(address.ToString());

    if (address.ToString() != null)

    break;

    }

    </quote>

    Friday, November 03, 2006 7:40 AM
  • Hi Friend,

     

    I tried your code on my application, but this is not getting me anything.

    I have 5 pc's and on my pc inside localhost the program is running.

    Pc1 is my Pc.

    So after adding your code to the page, i run the code on my pc1 pc and i get my MAC address.

     

    But when i run the application from any other pc other than pc1, i get the MAC address of pc1 instead of their own MAC id.

     

    What could be the Problem??

     

    Please help me out

     

    My Project is Stuck.

     

    This is a essential

    Monday, June 25, 2007 9:09 AM
  • Hi Ahmed

    I'm trying to get the MAC of my on computer but with your code I'm getting an exception The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) here's your code

     ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");
            StringBuilder theQueryBuilder = new StringBuilder(); 
    
     theQueryBuilder.Append("SELECT MACAddress FROM Win32_NetworkAdapter");
                ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());
                ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
                ManagementObjectCollection theCollectionOfResults = theSearcher.Get(); //exception at this point
    
                foreach (ManagementObject theCurrentObject in theCollectionOfResults)
                {
                    string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();
                    MessageBox.Show(macAdd);
                }

    Thursday, December 03, 2009 5:33 PM
  • Hej Smith010,

    I was looking for some "send email" option on this forum but haven't found so I am writing here. "I Have similar problem but in some other issue, can you tell me how you have resolved this problem? I mean the  the "RPC server is unavailable. (Exception from HRESULT: 0x800706BA)" error.

    Will be appreciated for any help.
    Sunday, February 14, 2010 11:27 PM
  • How does that work in Windows 7.

    rudrakshainpune

    Wednesday, December 21, 2011 1:03 PM
  • I m getting this error at run time   

    Invalid parameter

    please help out of this..

    Tuesday, November 06, 2012 10:30 AM