Getting MAC Address

M

Michael Jackson

I'm using the following code to get the MAC Address from the PC. The actual
MAC Address of the PC is 8th and 9th in the list (following the code). How
do I get just the MAC Address I need? Why are there so many entries?


Dim objManagementClass As New ManagementClass(New
ManagementPath("Win32_NetworkAdapter"))

Dim colManagementObjects As ManagementObjectCollection =
objManagementClass.GetInstances

For Each objMO As ManagementObject In colManagementObjects

Me.lstGeneric.Items.Add(IIf(IsNothing(objMO("MACAddress")), "Nothing",
objMO("MACAddress")))

Next

Output from the code is:

Nothing
20:41:53:59:4E:FF
Nothing
50:50:54:50:30:30
33:50:6F:45:30:30
Nothing
Nothing
00:0D:61:26:94:C8
00:0D:61:26:94:C8
62:FF:20:52:41:53
Nothing

Thanks,
Michael
 
G

Guest

using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.IO;

namespace GetMac
{
/// <summary>
/// Summary description for clsGetMac.
/// </summary>
public class clsGetMac
{
public clsGetMac()
{
//
// TODO: Add constructor logic here
//
}

public static string GetMac(string IP)
{
string str1=String.Empty;
try
{
string str2=String.Empty;
ProcessStartInfo info1 = new ProcessStartInfo();
Process process1 = new Process();
info1.FileName = "nbtstat";
info1.RedirectStandardInput = false;
info1.RedirectStandardOutput = true;
info1.Arguments = "-A " + IP;
info1.UseShellExecute = false;
info1.CreateNoWindow=true;
process1 = Process.Start(info1);
int num1 = -1;
while (num1 <= -1)
{
num1 = str2.Trim().ToLower().IndexOf("mac address", 0);
if (num1 > -1)
{
break;
}
str2 = process1.StandardOutput.ReadLine();
}
process1.WaitForExit();
str1 = str2.Trim();
}
catch (Exception exception2)
{
throw exception2;
}
return str1;
}


}
}
 
P

Phil Wilson

I suggest you look at some of the other attributes to filter out what you
want, such as AdappterType. You're seeing all the network providers
available. What you want is something like a NetConnectionID value of "Local
Area Cionnection".
 
M

Michael Jackson

Thanks for you help. I acutally used IPEnabled to filter. Seems to work.

Michael


Phil Wilson said:
I suggest you look at some of the other attributes to filter out what you
want, such as AdappterType. You're seeing all the network providers
available. What you want is something like a NetConnectionID value of
"Local Area Cionnection".
--
Phil Wilson
[Microsoft MVP-Windows Installer]

Michael Jackson said:
I'm using the following code to get the MAC Address from the PC. The
actual MAC Address of the PC is 8th and 9th in the list (following the
code). How do I get just the MAC Address I need? Why are there so many
entries?


Dim objManagementClass As New ManagementClass(New
ManagementPath("Win32_NetworkAdapter"))

Dim colManagementObjects As ManagementObjectCollection =
objManagementClass.GetInstances

For Each objMO As ManagementObject In colManagementObjects

Me.lstGeneric.Items.Add(IIf(IsNothing(objMO("MACAddress")), "Nothing",
objMO("MACAddress")))

Next

Output from the code is:

Nothing
20:41:53:59:4E:FF
Nothing
50:50:54:50:30:30
33:50:6F:45:30:30
Nothing
Nothing
00:0D:61:26:94:C8
00:0D:61:26:94:C8
62:FF:20:52:41:53
Nothing

Thanks,
Michael
 

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