Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had write a code to set the ip to a pc, but it works fine when I am running application from code, but problem is when I publish the web site, it shows the IP set successfully, but actually it not. pls help.

My code is,

VB
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim i As Integer
       i = Set_IP("10.17.1.148", "255.255.240.0", "10.17.1.21")
       If i = 1 Then
           Label1.Text = "IP SET"
       Else
           Label1.Text = "Fail To Set !"
       End If
   End Sub


   Public Function Set_IP(ByVal IPAddress As String, ByVal SubnetMask As String, _
                    ByVal Gateway As String)
       Dim managementClass As New ManagementClass("Win32_NetworkAdapterConfiguration")
       Dim mgObjCollection As ManagementObjectCollection = managementClass.GetInstances()
       Dim Flag As Boolean

       For Each mgObject As ManagementObject In mgObjCollection
           If Not CType(mgObject("IPEnabled"), Boolean) Then Continue For

           Try
               Dim objNewIP As ManagementBaseObject = Nothing
               Dim objSetIP As ManagementBaseObject = Nothing
               Dim objNewGate As ManagementBaseObject = Nothing

               objNewIP = mgObject.GetMethodParameters("EnableStatic")
               objNewGate = mgObject.GetMethodParameters("SetGateways")

               ' Set the default gateway (decided to declare and initialise
               ' variables rather than attempting to initialize the array
               ' while communicating with the WMI.
               Dim tmpStrArray() As String = {Gateway}

               objNewGate("DefaultIPGateway") = tmpStrArray
               Dim tmpIntArray() As Integer = {1}
               objNewGate("GatewayCostMetric") = tmpIntArray

               ' Set the IP address and subnet.
               tmpStrArray(0) = IPAddress
               objNewIP("IPAddress") = tmpStrArray
               tmpStrArray(0) = SubnetMask
               objNewIP("SubnetMask") = tmpStrArray

               objSetIP = mgObject.InvokeMethod("EnableStatic", objNewIP, Nothing)
               objSetIP = mgObject.InvokeMethod("SetGateways", objNewGate, Nothing)
               Flag = True
           Catch ex As Exception
               'MessageBox.Show("An error occured: " + ex.Message)
               Flag = False
           End Try
       Next
       If Flag = True Then
           Return 1
       Else
           Return 0
       End If
   End Function
Posted
Updated 5-Jul-11 2:54am
v6

1 solution

WHY are you trying to set the IP address of the server, and WHAT makes you think you can?
 
Share this answer
 
Comments
siddhai12 5-Jul-11 9:00am    
Actually I have developed hardware unit for access control, for updating or configuring of IP of that particular device I need this facility. Pls Help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900