CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Changing IP-Adress to a specified NIC

    Hello all,

    I am trying to change the IP-Adress, Subnet and the Gateway of only one NIC, using a c#-Programm.
    By searching the web I found one solution for changing all NICs of the client, but I need only to change the IP-Adress of my LAN adapter (not the wireless!).
    Does somebody know how i can update only a specified LAN-Adapter?
    Currently I have this code:
    Code:
            public void setIP(string ip_address, string subnet_mask, string nicName)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMC.GetInstances();
                foreach (ManagementObject objMO in objMOC)
                {
                    if ((bool)objMO["IPEnabled"])
                    {
                        try
                        {
                            if (objMO["Caption"].Equals(nicName))  //This is not working
                            {
                                ManagementBaseObject setIP;
                                ManagementBaseObject newIP =
                                objMO.GetMethodParameters("EnableStatic");
                                newIP["IPAddress"] = new string[] { ip_address };
                                newIP["SubnetMask"] = new string[] { subnet_mask };
                                setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
                                return;
                            }
                        }
                        catch (Exception)
                        {}
                    }
                }
            }
    here the cal for the function above:
    Code:
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                Console.WriteLine("Anzahl an Netzwerkkarten : {0}", nics.Length); 
                foreach (NetworkInterface adapter in nics) 
                {
                    if (adapter.NetworkInterfaceType.ToString() == "Ethernet")
                    {
                        if (adapter.Name.IndexOf("Drahtlose") < 0)
                        {
                            IPInterfaceProperties properties = adapter.GetIPProperties();
                            netzwerk.setIP("192.xxx.x.xx", "255.255.255.0", adapter.Name);
                        }
                   }
                }
    Many thanks for your help and best regards.
    Last edited by BioPhysEngr; March 6th, 2012 at 02:08 AM. Reason: added code and /code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured