Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

Win32_NetworkAdapter Array ToString()???

I would like ONE long continuous string of ALL MAC Addresses instead of several individual ones provided by the following method.

ManagementObjectSearcher find = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
            foreach (ManagementObject mo in find.Get())
            {
                string NicMacAddr = (mo["MACAddress"].ToString());              
            }


ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr
kvnsdr

ASKER

Can't get it to work. I was thinking about:

foreach (ManagementObject mo in find.Get())
            {
                string[] NicMacAddr = (mo["MACAddress"].ToString());
            }
What is an error you are getting?
Avatar of kvnsdr

ASKER

"Object reference not set to the instance of an object."

 NicMacAddr += (NicMacAddr.Length == 0 ? "; " : "") + (mo["MACAddress"].ToString());
Did you include the first line above the loop to define the variable as a string?
Avatar of kvnsdr

ASKER

oh yes, just didn't include in comment.
If the code you provided initially was working, then the changes I showed would give the results you want.  But it is possible tha tthe error is from that code...
Avatar of kvnsdr

ASKER

I find it interesting that my code gives the following results.

Notice the duplicate MACs and the blanks that are probably throwing an error with your code.

I only have 2 NICs in the test machine........

I've been using a BREAK; after my code to prevent errors........

MAC Address... D1:FD:04:5E:2D:A4
MAC Address...
MAC Address...
MAC Address... 40:50:44:51:20:20
MAC Address... 23:50:6E:55:20:20
MAC Address...
MAC Address...
MAC Address... 20:02:20:51:42:23
MAC Address... 00:0A:D1:7C:E6:CA
MAC Address... 00:0A:D1:7C:E6:CA