tags:

views:

940

answers:

4

I am building a pretty basic form app.

I can get a list of IP addresses available on the local machine. However, I want to also determine how these addresses are obtained (e.g. DHCP or static). How can I tell if a static IP address is configured on the system?

The goal is to inform a novice end-user (who may have no knowledge of the network setup, or how to obtain it) what static IP addresses are available. And, if no static address exist, inform them that one needs to be setup.

TIA

+2  A: 

You can use WMI to get network adapter configuration.

For an example, have a look at http://www.codeproject.com/KB/system/cstcpipwmi.aspx. The 'DhcpEnabled' property on the network adapter should tell you if the address is obtained via dhcp or not.

Nader Shirazie
Curse my slow typing skills!
Anderson Imes
haha... at least you took the time to write out the code...
Nader Shirazie
+1  A: 

Unfortunately you'll probably have to use WMI. There might be another way, but this is the only way that I know.

This code will output all of the information about every adapter on your system. I think the name is "DHCPEnabled" of the property you want.

        ManagementObjectSearcher searcherNetwork =
        new ManagementObjectSearcher("root\\CIMV2",
        "SELECT * FROM Win32_NetworkAdapterConfiguration");

        foreach (ManagementObject queryObj in searcherNetwork.Get())
        {
            foreach (var prop in queryObj.Properties)
            {
                Console.WriteLine(string.Format("Name: {0} Value: {1}", prop.Name, prop.Value));
            }
        }
Anderson Imes
This required a little work, but was basically what I was looking for. Thanks!
robotshapes
A: 

There is LINQ to WMI api to,
[1]http://www.codeplex.com/linq2wmi
use this if you want

ArsenMkrt
A: 

The answers here helped me with my own project, but I had to do some research before I found out how to use the suggested method.

Adding using System.Management; to your code doesn't work by itself. You need to add a reference to System.Management before the namespace will be recognized. (For new people like me who tried this and were getting the error "managementclass could not be found").

JYelton