none
How to get the same output as the one got while trying the ipconfig command? RRS feed

  • Question

  • I am trying to get the same list of IP addresses displayed by "ipconfig". I am using .NET to achieve this. What I have tried so far.

    foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses; if (uniCast.Count >0) { Console.WriteLine(adapter.Description); string lifeTimeFormat = "dddd, MMMM dd, yyyy hh:mm:ss tt"; foreach (UnicastIPAddressInformation uni in uniCast) { DateTime when; Console.WriteLine(" Unicast Address ......................... : {0}", uni.Address);

    }

    }

    }

    This achieves what I need. But for few interfaces it returns some random IP addresses which are not found for the same adapter when I do a ipconfig/all from command prompt. I came to know that these are the link local IPv4 addresses which are obtained by the hosts via auto configuration when the dhcp server is not available. This belongs to the 169.254.0.0/16 series. Is there a .NET method / property in the network information class which will help me filter this address from the IP list.



    • Edited by Sudharsan62 Monday, September 11, 2017 4:02 AM
    Monday, September 11, 2017 3:26 AM

All replies

  • Hi Sudharsan62,

    Thank you for posting here.

    If you want to get the all return from the ipconfig, you could try the following code.

      static void Main(string[] args)
            {
               
                Console.WriteLine(GetIPConfigReturns());
                
                Console.ReadKey();
    
            }
            public static string GetIPConfigReturns()
            {
                string version = System.Environment.OSVersion.VersionString;
    
                if (version.Contains("Windows"))
                {
                   
                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("ipconfig", "/all");
    
                    psi.CreateNoWindow = true; 
                    psi.RedirectStandardOutput = true;
                    psi.UseShellExecute = false;
    
                    System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
    
                    return p.StandardOutput.ReadToEnd();
                }
    
                return string.Empty;
            }

    For security, I cover some personal information.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 12, 2017 3:10 AM
    Moderator