如何设置连接到主机设备的IP地址? C#Winform的? [英] How to set IP address of device connected to Host PC? C# Winform?

查看:242
本文介绍了如何设置连接到主机设备的IP地址? C#Winform的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Modbus TCP / IP到MODBUS RTU转换器,它带有默认的192.168.0.1的IP。我需要开发一个小的C#winform应用程序到该设备的IP地址更改为任何所需的IP地址。我该怎么做了。

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?.

推荐答案

您可以使用WMI做到这一点(的 Windows管理规范)。

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

首先,你必须添加为<$ C参考$ C> System.Management 到项目中。

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

其次,你需要找到的NetworkInterface 按名称您的网络连接:

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;
    }



第三,你必须创建一个的ManagementObject 的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;
    }



四号位,可以设置与ipadress:

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...

这篇关于如何设置连接到主机设备的IP地址? C#Winform的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆