Checking a static or dynamic IP address in C # .NET?

I am creating a fairly simple form application.

I can get a list of the IPs available on the local machine. However, I want to also define how these addresses will be obtained (eg DHCP or static). How do I know if a static IP address is configured on the system?

The goal is to inform the novice end user (who may not know the network setup or how to get one) what static IP addresses are available. And, if the static address doesn't exist, tell them what to configure.

TIA

+1


a source to share


5 answers


Unfortunately, you probably have to use WMI. Maybe there is another way, but this is the only way I know.

This code will print all information about each adapter on your system. I think this is the name "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));
            }
        }

      

+1


a source


using System.Net.NetworkInformation;

NetworkInterface[] niAdpaters = NetworkInterface.GetAllNetworkInterfaces();

private Boolean GetDhcp(Int32 iSelectedAdpater)
{
    if (niAdpaters[iSelectedAdpater].GetIPProperties().GetIPv4Properties() != null)
    {
        return niAdpaters[iSelectedAdpater].GetIPProperties().GetIPv4Properties().IsDhcpEnabled;
    }
    else
    {
        return false;
    }
}

      



+9


a source


You can use WMI to get the configuration of the network adapter.

Consider http://www.codeproject.com/KB/system/cstcpipwmi.aspx as an example . The "DhcpEnabled" property on the network adapter should tell you if the address is received via dhcp or not.

+2


a source


I use two methods as follows:

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());

    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();

        }

    }

    return "unknown";
}

public static string GetLocalIpAllocationMode()
{
    string MethodResult = "";
    try
    {
        ManagementObjectSearcher searcherNetwork = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration");

        Dictionary<string, string> Properties = new Dictionary<string, string>();

        foreach (ManagementObject queryObj in searcherNetwork.Get())
        {
            foreach (var prop in queryObj.Properties)
            {
                if (prop.Name != null && prop.Value != null && !Properties.ContainsKey(prop.Name))
                {
                    Properties.Add(prop.Name, prop.Value.ToString());

                }

            }

        }

        MethodResult = Properties["DHCPEnabled"].ToLower() == "true" ? "DHCP" : "Static";

    }
    catch (Exception ex)
    {
        ex.HandleException();

    }

    return MethodResult;

}

      

GetLocalIpAllocationMode()

will tell you whether ip

static

or not allocated dhcp

, whereas GetLocalIPAddress()

will tell you local ip

.

+1


a source


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

Adding with help System.Management;

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

0


a source







All Articles