I am writing a Windows Forms application in VB.Net, which, in particular, will change the IP address, Default Gateway, Subnet Mask and set the IP address to Static only on the Winfows 7 image. Sysprep is not used. I searched Google and only came up with 2 options. I do not believe that the first solution will work for me, because I do not necessarily know the name of the connection. It uses netsh to change IP settings. I was going to give a link to this example, but I can’t post more than two links ...
The second solution is shown on this link (VB.Net version) , and the source code is here (C # version) . This solution uses WMI, which I really know nothing about.
When I debug the code and look through everything, the code seems to be running correctly, but the IP address is still set to DHCP, and all other settings remain the same. So basically what gives? Why is this code not working?
Here is my code. I made only a few changes:
'Changed the 3 IPs below
Dim IPAddress As String = "192.168.1.105"
Dim SubnetMask As String = "255.255.252.0"
Dim Gateway As String = "192.168.1.100"
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If (Not CBool(objMO("IPEnabled"))) Then
Continue For
End If
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewGate As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableStatic")
objNewGate = objMO.GetMethodParameters("SetGateways")
'Set DefaultGateway
objNewGate("DefaultIPGateway") = New String() {Gateway}
objNewGate("GatewayCostMetric") = New Integer() {1}
'Set IPAddress and Subnet Mask
objNewIP("IPAddress") = New String() {IPAddress}
objNewIP("SubnetMask") = New String() {SubnetMask}
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)
'Changed this line so I could see if it was executing all of the way
MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway!")
Catch ex As Exception
MessageBox.Show("Unable to Set IP : " & ex.Message)
End Try
Next objMO
source
share