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
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)