none
How to change IP Address of local system using c#

    Domanda

  • How to change IP Address of local system using c# as i want that my application should be able to change local systems IP Address within some interval of time. How to do that with c# ?
    martedì 17 gennaio 2012 08:37

Risposte

  • Hi,

    Please try this code, I found on one forum... to change the IP using CSharp

     

    using System; 
    using System.Management; 
     
    namespace WindowsFormsApplication_CS 
    { 
        class NetworkManagement 
        { 
            /// <summary> 
            /// Set's a new IP Address and it's Submask of the local machine 
            /// </summary> 
            /// <param name="ip_address">The IP Address</param> 
            /// <param name="subnet_mask">The Submask IP Address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setIP(string ip_address, string subnet_mask) 
            { 
                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"); 
     
                            newIP["IPAddress"] = new string[] { ip_address }; 
                            newIP["SubnetMask"] = new string[] { subnet_mask }; 
     
                            setIP = objMO.InvokeMethod("EnableStatic", newIP, null); 
                        } 
                        catch (Exception) 
                        { 
                            throw; 
                        } 
     
     
                    } 
                } 
            } 
            /// <summary> 
            /// Set's a new Gateway address of the local machine 
            /// </summary> 
            /// <param name="gateway">The Gateway IP Address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setGateway(string gateway) 
            { 
                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[] { gateway }; 
                            newGateway["GatewayCostMetric"] = new int[] { 1 }; 
     
                            setGateway = objMO.InvokeMethod("SetGateways", newGateway, null); 
                        } 
                        catch (Exception) 
                        { 
                            throw; 
                        } 
                    } 
                } 
            } 
            /// <summary> 
            /// Set's the DNS Server of the local machine 
            /// </summary> 
            /// <param name="NIC">NIC address</param> 
            /// <param name="DNS">DNS server address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setDNS(string NIC, string DNS) 
            { 
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
                ManagementObjectCollection objMOC = objMC.GetInstances(); 
     
                foreach (ManagementObject objMO in objMOC) 
                { 
                    if ((bool)objMO["IPEnabled"]) 
                    { 
                        if (objMO["Caption"].Equals(NIC)) 
                        { 
                            try 
                            { 
                                ManagementBaseObject newDNS = 
                                    objMO.GetMethodParameters("SetDNSServerSearchOrder"); 
                                newDNS["DNSServerSearchOrder"] = DNS.Split(','); 
                                ManagementBaseObject setDNS = 
                                    objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); 
                            } 
                            catch (Exception) 
                            { 
                                throw; 
                            } 
                        } 
                    } 
                } 
            } 
            /// <summary> 
            /// Set's WINS of the local machine 
            /// </summary> 
            /// <param name="NIC">NIC Address</param> 
            /// <param name="priWINS">Primary WINS server address</param> 
            /// <param name="secWINS">Secondary WINS server address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setWINS(string NIC, string priWINS, string secWINS) 
            { 
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
                ManagementObjectCollection objMOC = objMC.GetInstances(); 
     
                foreach (ManagementObject objMO in objMOC) 
                { 
                    if ((bool)objMO["IPEnabled"]) 
                    { 
                        if (objMO["Caption"].Equals(NIC)) 
                        { 
                            try 
                            { 
                                ManagementBaseObject setWINS; 
                                ManagementBaseObject wins = 
                                objMO.GetMethodParameters("SetWINSServer"); 
                                wins.SetPropertyValue("WINSPrimaryServer", priWINS); 
                                wins.SetPropertyValue("WINSSecondaryServer", secWINS); 
     
                                setWINS = objMO.InvokeMethod("SetWINSServer", wins, null); 
                            } 
                            catch (Exception) 
                            { 
                                throw; 
                            } 
                        } 
                    } 
                } 
            }  
        } 
    }
    

    Please try and let us know :

    I have not checked the code so not proposing as answer... do mark as answer if it solves your purpose.

    Using this class use a Timer on your form to change your IP address as per your timing conventions.. it might work for you..

    Remember to add reference to assembly System.Management by rightclicking on the project. This example will set IP if your IP configuration is enabled on your network interface card. Means you should have manually set IP Address and Gateways not to set as Automatically obtain IP Address.

     


    Please mark as answer if it helps you or unpropose as answer if it does not help you at all.

     

    Thanks

    Rehan Bharucha - The Tech Robot

    MCTS, MCITP, MCPD, MCT, MCC






    giovedì 19 gennaio 2012 07:43

Tutte le risposte

  • Try the below links. They give you very good explanation of chaning IP address with code,

    http://stackoverflow.com/questions/209779/how-can-you-change-network-settings-ip-address-dns-wins-host-name-with-code

    http://www.codeproject.com/KB/miscctrl/IpAddrCtrlLib.aspx

    http://www.codeproject.com/KB/IP/ChangingIPADDRESS.aspx

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!
    martedì 17 gennaio 2012 08:46
  • Thanks dear...

    I already checked them out but still not satisfied !!

    martedì 17 gennaio 2012 09:05
  • Hi Rumman,

    Could you please provide us with more information about why you are no satisfied with the solutions here?   Do they not work or something else?

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    giovedì 19 gennaio 2012 07:20
  • Hi,

    Please try this code, I found on one forum... to change the IP using CSharp

     

    using System; 
    using System.Management; 
     
    namespace WindowsFormsApplication_CS 
    { 
        class NetworkManagement 
        { 
            /// <summary> 
            /// Set's a new IP Address and it's Submask of the local machine 
            /// </summary> 
            /// <param name="ip_address">The IP Address</param> 
            /// <param name="subnet_mask">The Submask IP Address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setIP(string ip_address, string subnet_mask) 
            { 
                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"); 
     
                            newIP["IPAddress"] = new string[] { ip_address }; 
                            newIP["SubnetMask"] = new string[] { subnet_mask }; 
     
                            setIP = objMO.InvokeMethod("EnableStatic", newIP, null); 
                        } 
                        catch (Exception) 
                        { 
                            throw; 
                        } 
     
     
                    } 
                } 
            } 
            /// <summary> 
            /// Set's a new Gateway address of the local machine 
            /// </summary> 
            /// <param name="gateway">The Gateway IP Address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setGateway(string gateway) 
            { 
                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[] { gateway }; 
                            newGateway["GatewayCostMetric"] = new int[] { 1 }; 
     
                            setGateway = objMO.InvokeMethod("SetGateways", newGateway, null); 
                        } 
                        catch (Exception) 
                        { 
                            throw; 
                        } 
                    } 
                } 
            } 
            /// <summary> 
            /// Set's the DNS Server of the local machine 
            /// </summary> 
            /// <param name="NIC">NIC address</param> 
            /// <param name="DNS">DNS server address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setDNS(string NIC, string DNS) 
            { 
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
                ManagementObjectCollection objMOC = objMC.GetInstances(); 
     
                foreach (ManagementObject objMO in objMOC) 
                { 
                    if ((bool)objMO["IPEnabled"]) 
                    { 
                        if (objMO["Caption"].Equals(NIC)) 
                        { 
                            try 
                            { 
                                ManagementBaseObject newDNS = 
                                    objMO.GetMethodParameters("SetDNSServerSearchOrder"); 
                                newDNS["DNSServerSearchOrder"] = DNS.Split(','); 
                                ManagementBaseObject setDNS = 
                                    objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); 
                            } 
                            catch (Exception) 
                            { 
                                throw; 
                            } 
                        } 
                    } 
                } 
            } 
            /// <summary> 
            /// Set's WINS of the local machine 
            /// </summary> 
            /// <param name="NIC">NIC Address</param> 
            /// <param name="priWINS">Primary WINS server address</param> 
            /// <param name="secWINS">Secondary WINS server address</param> 
            /// <remarks>Requires a reference to the System.Management namespace</remarks> 
            public void setWINS(string NIC, string priWINS, string secWINS) 
            { 
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
                ManagementObjectCollection objMOC = objMC.GetInstances(); 
     
                foreach (ManagementObject objMO in objMOC) 
                { 
                    if ((bool)objMO["IPEnabled"]) 
                    { 
                        if (objMO["Caption"].Equals(NIC)) 
                        { 
                            try 
                            { 
                                ManagementBaseObject setWINS; 
                                ManagementBaseObject wins = 
                                objMO.GetMethodParameters("SetWINSServer"); 
                                wins.SetPropertyValue("WINSPrimaryServer", priWINS); 
                                wins.SetPropertyValue("WINSSecondaryServer", secWINS); 
     
                                setWINS = objMO.InvokeMethod("SetWINSServer", wins, null); 
                            } 
                            catch (Exception) 
                            { 
                                throw; 
                            } 
                        } 
                    } 
                } 
            }  
        } 
    }
    

    Please try and let us know :

    I have not checked the code so not proposing as answer... do mark as answer if it solves your purpose.

    Using this class use a Timer on your form to change your IP address as per your timing conventions.. it might work for you..

    Remember to add reference to assembly System.Management by rightclicking on the project. This example will set IP if your IP configuration is enabled on your network interface card. Means you should have manually set IP Address and Gateways not to set as Automatically obtain IP Address.

     


    Please mark as answer if it helps you or unpropose as answer if it does not help you at all.

     

    Thanks

    Rehan Bharucha - The Tech Robot

    MCTS, MCITP, MCPD, MCT, MCC






    giovedì 19 gennaio 2012 07:43
  • Is it necessary to change subnet mask and other things with ip address...??
    giovedì 26 gennaio 2012 15:00