Tyler Adams

Tyler Adams

  • NA
  • 10
  • 0

Unable to get IP address using C# and WMI

Mar 23 2009 4:34 PM

I'm working on an InfoPath form that is going to have a repeating table.  In this form i'm using C# and WMI to retrieve hardware information for an inventory database.  I've managed to figure out how to populate each row with the Network Interface and MAC Address, but i'm having difficulty when trying to get the IP Address, and I suspect i'll have the same problem trying to get the Subnet Mask, so if i can solve one i'll be able to solve the other. 

The code i'm using is listed below (i stripped out the other code in there for the hardware not related to the Network Info), but when i use this code, in the IP Address field, it shows "System.String[]" instead of displaying the actual IP Address.  Can some one please help me?  I got my code samples from the WMI Code Creator and manipulated it to fit in with my existing code.  It's pretty ugly right now, but so long as it works i dont care.

using Microsoft.Office.InfoPath;

using System;

using System.Xml;

using System.Xml.XPath;

using System.Management;

namespace Inventory_Rev1

{

public partial class FormCode

{

ManagementObjectSearcher searcherNetwork =

new ManagementObjectSearcher("root\\CIMV2",

"SELECT * FROM Win32_NetworkAdapterConfiguration");

public void InternalStartup()

{

EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);

}

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

foreach (ManagementObject queryObj in searcherNetwork.Get())

{

int counter = 1;

//while (rows.MoveNext())

{

string nicName = queryObj["Description"].ToString();

string nicMac = (queryObj.Properties["MACAddress"].Value != null) ? queryObj.Properties["MACAddress"].Value.ToString() : "";

string nicIp = (queryObj.Properties["IPAddress"].Value != null) ? queryObj.Properties["IPAddress"].Value.ToString() : "";

// Create an XPathNavigator to walk the main data source

// of the form.

XPathNavigator xnMyForm = this.CreateNavigator();

XmlNamespaceManager ns = this.NamespaceManager;

xnMyForm.SelectSingleNode("/my:myFields/my:Network/my:AdapterInfo/my:nicName", ns)

.SetValue(nicName);

xnMyForm.SelectSingleNode("/my:myFields/my:Network/my:AdapterInfo/my:nicMac", ns)

.SetValue(nicMac);

xnMyForm.SelectSingleNode("/my:myFields/my:Network/my:AdapterInfo/my:nicIp", ns)

.SetValue(nicIp);

// Increment the counter

counter++;

// Add the item to the repeating table

AddItem(nicName, nicMac, nicIp);

 

// Remove the first empty item from the repeating table

DeleteFirstEmptyItemNIC();

}

}

}

private void AddItem(string nicName, string nicMac, string nicIp)

{

XmlDocument doc = new XmlDocument();

XmlNode group = doc.CreateElement("AdapterInfo",

NamespaceManager.LookupNamespace("my"));

XmlNode field = doc.CreateElement("nicName",

NamespaceManager.LookupNamespace("my"));

XmlNode node = group.AppendChild(field);

node.InnerText = nicName;

field = doc.CreateElement("nicMac",

NamespaceManager.LookupNamespace("my"));

node = group.AppendChild(field);

node.InnerText = nicMac;

field = doc.CreateElement("nicIp",

NamespaceManager.LookupNamespace("my"));

node = group.AppendChild(field);

node.InnerText = nicIp;

doc.AppendChild(group);

MainDataSource.CreateNavigator().SelectSingleNode(

"/my:myFields/my:Network",

NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

 

}

private void DeleteFirstEmptyItemNIC()

{

XPathNavigator domNav = MainDataSource.CreateNavigator();

XPathNavigator itemNav = domNav.SelectSingleNode(

"/my:myFields/my:group1/my:group2[1]",

NamespaceManager);

if (itemNav != null)

itemNav.DeleteSelf();

}

}

}


Answers (1)