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

老子叫甜甜 提交于 2019-12-23 02:35:01

问题


I have a Modbus TCP/IP to MODBUS RTU converter , which comes with a default IP of 192.168.0.1 . I need to develop a small c# Winform app to change this device's IP address to any desired IP address. How do I do that?.


回答1:


You could do it with WMI (Windows Management Instrumentation).

First, you have to add the reference for System.Management to your Project.

Second, you need to find the 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 have to create a 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;
    }

Fours, you can set the 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 a 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 policies on your pc, may be you have to run the program as Administrator...



来源:https://stackoverflow.com/questions/34333683/how-to-set-ip-address-of-device-connected-to-host-pc-c-sharp-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!