How to set the IP address of the device connected to the PC? C # Winform?

I have a Modbus TCP / IP to MODBUS RTU converter that comes with a default IP address of 192.168.0.1. I need to develop a small C # Winform application to change the IP address of this device to whatever IP address I want. How to do it?

+1


source to share


1 answer


You can do this using WMI ( Windows Management Instrumentation ).

You must first add a link to System.Management

your project.

Second, you need to search NetworkInterface

for your network connection by name:

using System.Net.NetworkInformation;
using System.Management;

public class NetworkManager
{
    public static NetworkInterface GetNetworkInterface(string sName)
    {
        NetworkInterface NetInterface = null;

        // Precondition
        if (sName == "") return null;

        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in interfaces)
        {
            if (ni.Name == sName)
            {
                NetInterface = ni;
                break;
            }
        }

        return NetInterface;
    }

      

Third, you must create ManagementObject

for your NetworkInterface

:

    public static ManagementObject GetNetworkAdapterManagementObject(NetworkInterface NetInterface)
    {
        ManagementObject oMngObj = null;

        // Precondition
        if (NetInterface == null) return null;

        string sNI = NetInterface.Id;

        ManagementClass oMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection oMOC = oMC.GetInstances();

        foreach (ManagementObject oMO in oMOC)
        {
            string sMO = oMO["SettingID"].ToString();
            if (sMO == sNI)
            {
                // Found
                oMngObj = oMO;
                break;
            }
        }

        return oMngObj;
    }

      



Four, you can install ipadress with:

    public static bool SetIPAdress(ManagementObject oMO, string[] saIPAdress, string[] saSubnetMask)
    {
        bool bErg = false;
        try
        {
            // Precondition
            if (oMO == null) return false;
            if (saIPAdress == null) return false;
            if (saSubnetMask == null) return false;

            // Get ManagementBaseObject 
            ManagementBaseObject oNewIP = null;
            oNewIP = oMO.GetMethodParameters("EnableStatic");

            oNewIP["IPAddress"] = saIPAdress;
            oNewIP["SubnetMask"] = saSubnetMask;

            // Invoke
            oMO.InvokeMethod("EnableStatic", oNewIP, null);

            // Alles ok
            bErg = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine("SetIPAdress failed: " + ex.Message);
        }

        return bErg;
    }
}

      

Now you can use it, for example, in your button click event handler:

private void button1_Click(object sender, EventArgs e)
{
    string sConn = "LAN-Verbindung";
    NetworkInterface oNI = NetworkManager.GetNetworkInterface(sConn);
    ManagementObject oMO = NetworkManager.GetNetworkAdapterManagementObject(oNI);

    string sIPAdress = "192.168.1.1";
    string sSubnetMask = "255.255.255.0";
    string[] saIPAdress = {sIPAdress};
    string[] saSubnetMask = {sSubnetMask};
    if (NetworkManager.SetIPAdress(oMO, saIPAdress, saSubnetMask))
    {
        Console.WriteLine("Yes...");
    }
}

      

Depending on the policy on your PC, you may need to run the program as Administrator ...

+1


source







All Articles