Changing IP-Adress to a specified NIC
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Changing IP-Adress to a specified NIC

  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

  2. #2
    Join Date
    Mar 2012
    Posts
    1

    Re: Changing IP-Adress to a specified NIC

    Can we change MAC address ?

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: Changing IP-Adress to a specified NIC

    Hello,

    Please surround code segments with [code] and [/code] tags to preserve formatting and make it easier to read. I've fixed your current post for now.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Mar 2012
    Posts
    3

    Re: Changing IP-Adress to a specified NIC

    Hello BioPhysEngr,

    sorry for that and thanks for fixing.

    @uwillfeelsorry1:
    It would be also ok having a possibility to change all Ethernet-Adapters, except the wireless.
    With this function I can identify the network-adapters with the needed information to know if itīs the right network adapter. But I have trouble to merge this codes together.
    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)
             { netzwerk.setIP("192.168.1.11", "255.255.255.0", adapter.Name);  }
        }
    }

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center