简体   繁体   中英

setup other computer network and configure the ip address programatically C#

I have some sample codes where I can change my own ip address.

Now i have a problem regarding changing the ip address of my networks. When i mean networks (Computers that are connected to the lan) I want to connect and configure their ip address as well.

Example

I have 2 computers and the ip addresses are 192.168.1.6 - Computer 1 and 192.168.1.7 - Computer 2 . So now I am at computer 1 and I want to connect to 192.168.1.7 and change the ip address to 192.168.1.8

CODE

private void button1_Click_1(object sender, EventArgs e)
{
//example of ip 10.11.3.120
    try
    {
        setIP();
        setGateway();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        setIP();
        setGateway();
        MessageBox.Show("Update Success");


    }
}

private void setIP()
{
    ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMC.GetInstances();

    foreach (ManagementObject objMO in objMOC)
    {
        if ((bool)objMO["IPEnabled"])
        {
            try
            {
                ManagementBaseObject setIP;
                ManagementBaseObject newIP =
                    objMO.GetMethodParameters("EnableStatic");
                //string ip_address = "10.11.3.120";
                //string subnet_mask = "255.255.255.0";
                newIP["IPAddress"] = new string[] { textBox1.Text };

                newIP["SubnetMask"] = new string[] { textBox2.Text };

                setIP = objMO.InvokeMethod("EnableStatic", newIP, null);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


        }
    }
}

public void setGateway()
{
    ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMC.GetInstances();

    foreach (ManagementObject objMO in objMOC)
    {
        if ((bool)objMO["IPEnabled"])
        {
            try
            {
                ManagementBaseObject setGateway;
                ManagementBaseObject newGateway =
                    objMO.GetMethodParameters("SetGateways");

                newGateway["DefaultIPGateway"] = new string[] { textBox3.Text };
                newGateway["GatewayCostMetric"] = new int[] { 1 };

                setGateway = objMO.InvokeMethod("SetGateways", newGateway, null);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

This is not a direct answer. However, you should have all the information you need here

Connecting to WMI Remotely with C#

In short you will need create a system ManagementScope

Note System.Management was the original .NET namespace used to access WMI; however, the APIs in this namespace generally are slower and do not scale as well relative to their more modern Microsoft.Management.Infrastructure counterparts.

However

  1. Create a ManagementScope object, using the name of the computer and the WMI path, and connect to your target with a call to ManagementScope.Connect() .

  2. If you connect to a remote computer in a different domain or using a different user name and password, then you must use a ConnectionOptions object in the call to the ManagementScope .

The ConnectionOptions contains properties for describing the Authentication, Impersonation, username, password, and other connection options.

ConnectionOptions options = new ConnectionOptions();
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

// options takes more arguments, you need to read up on what you want

ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\cimv2", options);
scope.Connect();

ManagementPath path = new ManagementPath("Win32_NetworkAdapterConfiguration");
ObjectGetOptions o = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
ManagementClass objMC = new ManagementClass(scope, path, o);
...

Generally speaking, it is recommended that you set your Impersonation level to Impersonate unless explicitly needed otherwise


Additional reading

Connecting to WMI Remotely with C#

ManagementScope Class

ConnectionOptions Class

ObjectGetOptions Class

ManagementPath Class

ManagementClass Class

Disclaimer : You will have to read about these topics and work out what you need in your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM