Get MAC Address Problem

  • Thread starter Thread starter david
  • Start date Start date
D

david

Hi All,

I have to get the MAC address in my program. But I didn't have the correct
property name of management object. And I don't know how to get it. Can some
one help me?

Below is my code.

Thanks,



ManagementClass adapters = new ManagementClass("Win32_NetworkAdapter");

string mac = "";
adapters.Get();
int ix =adapters.Properties.Count;
ix = adapters.Properties.Count;
foreach (ManagementObject adapter in adapters.GetInstances())
{
mac = adapter["Name"].ToString() + " " ;
mac += adapter["MACAddress"].ToString(); <--- crashed here,
propertyname is wrong.



--
David



--
David
Computer Systems Int'l
Tel : 416-497-0370 x24
Fax: 416-497-6760
Toll Free: 1-888-836-7274
www.compsysint.com
(e-mail address removed)
 
adapter["MACAddress"] could be null

so test it before calling ToString()

if(adapter["MACAddress"] != null)
mac += adapter["MACAddress"].ToString();
 
Hello David,

Thanks for posting in the group.

Here is a code sample in newsgroup that we often used to get Mac address:

Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
MsgBox("MAC address " & mo.Item("MacAddress").ToString())
End If
Next

Hope that help.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "david" <[email protected]>
!Subject: Get MAC Address Problem
!Date: Wed, 1 Oct 2003 14:54:22 -0400
!Lines: 41
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <#[email protected]>
!Newsgroups: microsoft.public.dotnet.framework
!NNTP-Posting-Host: host-126.compsysint.com 216.191.176.126
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:55187
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!Hi All,
!
! I have to get the MAC address in my program. But I didn't have the
correct
!property name of management object. And I don't know how to get it. Can
some
!one help me?
!
!Below is my code.
!
!Thanks,
!
!
!
! ManagementClass adapters = new ManagementClass("Win32_NetworkAdapter");
!
! string mac = "";
! adapters.Get();
! int ix =adapters.Properties.Count;
! ix = adapters.Properties.Count;
! foreach (ManagementObject adapter in adapters.GetInstances())
! {
! mac = adapter["Name"].ToString() + " " ;
! mac += adapter["MACAddress"].ToString(); <--- crashed here,
!propertyname is wrong.
!
!
!
!--
!David
!
!
!
!--
!David
!Computer Systems Int'l
!Tel : 416-497-0370 x24
!Fax: 416-497-6760
!Toll Free: 1-888-836-7274
!www.compsysint.com
[email protected]
!
!
!
 
Back
Top