Getting MAC address/Physical IP address of machine through C#

P

Pioneer

Hi,

I need to get MAC address/physical ip address of machine. I am using
below code:

ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
string macAddr = mo["MACAddress"] as string;
if ( macAddr != null && macAddr.Trim() != "" )
return macAddr.ToString();
}

But this returns multiple IP addresses out of which Physical address
is shown twice and rest of the addresses I could not find in ipconfig/
all command output on cmd.

Could you help me understand the output?

I only want the physical address. IF there is any better alternative,
kindly let me know.

Thanks
Pioneer
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I need to get MAC address/physical ip address of machine. I am using
below code:

ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
string macAddr = mo["MACAddress"] as string;
if ( macAddr != null && macAddr.Trim() != "" )
return macAddr.ToString();

}

But this returns multiple IP addresses out of which Physical address
is shown twice and rest of the addresses I could not find in ipconfig/
all command output on cmd.

Could you help me understand the output?

I only want the physical address. IF there is any better alternative,
kindly let me know.

Thanks
Pioneer

I'm not very sure but I think you will get one per device, either
physical or virtual (ie loopback). I'm sure there is a property that
indicate which kind of device you are dealing with.
Check the docs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top