Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unable to get IP Address using WMI and C#

Unable to get IP Address using WMI and C#

Scheduled Pinned Locked Moved C#
xmlhelpcsharpdatabasesysadmin
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Buck_Murdock
    wrote on last edited by
    #1

    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

    0 L X 3 Replies Last reply
    0
    • B Buck_Murdock

      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

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      Look into the System.Net.NetworkInformation namespace, in particular the NetworkInterface.GetAllNetworkInterfaces method. For each one you can call the GetPhysicalAddressMethod. That should make your code cleaner (and faster). This will also allow access to each interface's IP address using the UnicastAddresses property of the GetIpProperties() method

      1 Reply Last reply
      0
      • B Buck_Murdock

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, from MSDN: Object.ToString() returns a String that represents the current Object. The default implementation returns the fully qualified name of the type of the Object. This method can be overridden in a derived class to return values that are meaningful for that type. So the output you are getting tells you it isn't a string, it is an array of strings; so have a closer look and discover how you may use that information to your advantage. Chances are string.Join() will come in handy. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        1 Reply Last reply
        0
        • B Buck_Murdock

          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

          X Offline
          X Offline
          Xmen Real
          wrote on last edited by
          #4

          here is the mistake

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

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

          IPAddress, MACAddress, IPSubnet...all returns string array and you are calling ToString() of string array here is a code snippet I used

                          string\[\] ipaddresses = (string\[\])netCI\["IPAddress"\];
                          string\[\] subnets = (string\[\])netCI\["IPSubnet"\];
          

          TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

          ----------------------------------------------- 128 bit encrypted signature, crack if you can

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups