Something like print_r (PHP) for .NET?

clamum

Lifer
Feb 13, 2003
26,252
403
126
Is there something like the PHP print_r function for .NET? I need to find out the index names of an array, more precisely from a WMI ManagementObject class. I shouldn't need to do this since I looked up the ManagementObject class members on MSDN but when trying to reference some of the indexes, say "Status", I get a null reference exception so I'm guessing the object I'm working with does not have that index.

The code that gets the information does a "SELECT * FROM Win32_NetworkAdapter" so I would think it should get all of the available members of NetworkAdapter ("Status" is one of them).

Any ideas? Thank you.
 

yinan

Golden Member
Jan 12, 2007
1,801
2
71
List the code after that please. I have a script that I use to inventory machines and I may be able to give you some code.

Jim
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
I just don't get why that ManagementObject doesn't have the "Status" index in it since according to the MSDN members, the Win32_NetworkAdapter does and I would think doing a "SELECT *" grabs all available members from it.

EDIT: Apparently the Attach Code doesn't recognize line breaks, sweet. Sorry.
 

yinan

Golden Member
Jan 12, 2007
1,801
2
71
It does grab all members, but what it returns is a collection not a single item.

Forgive the following code its in vbs
Code:
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
    For each objItem in colItems
        If objItem.Manufacturer <> "" and left(objItem.MACAddress, 1) = "0" then
            strMacAddress = strMacAddress & objItem.MACAddress & vbCrLf
            strNetworkAddress = strNetworkAddress & objItem.NetworkAddresses & vbCrLf
            strNetworkCard = strNetworkCard & objItem.ProductName & vbCrLf
        end If
    Next
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Jeez someone needs to fix this fscking Attach Code thing.

I know it's a collection and I loop through that (code below) but as you can see I check if the MACAddress is empty, and when I add in another condition, oReturn["Status"].ToString(), I get a null reference exception.

I would just like to get a list of all possible values in between those square brackets for a object in the collection, i.e. print out every possible index in between [ and ] so I can see what's being returned.

Code:
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select * from Win32_NetworkAdapter");
oSearcher = new ManagementObjectSearcher(oMs,oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();

foreach(ManagementObject oReturn in oReturnCollection) {
....if (oReturn["MACAddress"].ToString() != "") {
........csBO.scanheader.UniqueID = oReturn["MACAddress"].ToString();
........oReturn.Dispose();
........break;
....}
....else {
........oReturn.Dispose();
........continue;
....}
}
 

yinan

Golden Member
Jan 12, 2007
1,801
2
71
I got this to work in vb (I dont know C# so sorry but at least its a help)

Code:
ListBox1.Items.Clear()
        Dim wqlQuery As WqlObjectQuery
        Dim macAddress As String
        wqlQuery = New WqlObjectQuery("SELECT * FROM Win32_NetworkAdapter")

        Dim searcher As ManagementObjectSearcher
        searcher = New ManagementObjectSearcher(wqlQuery)

        Dim nic As ManagementObject
        For Each nic In searcher.Get()
            If nic("MacAddress") <> "" Then
                macAddress = nic("MacAddress")
                ListBox1.Items.Add(macAddress)
                Console.WriteLine(macAddress)
            End If
        Next

I think your problem was with the .ToString() I was getting errors when I called that method.