PDA

View Full Version : آموزش: تغییر Local Ip با کد نویسی



Lord_Voldemort
شنبه 02 آبان 1388, 04:44 صبح
من خیلی به این کد نیاز داشتم و بالاخره پیداش کردم و اونو میزازم تا دیگران مثل من واسه پیدا کردنش اذیت نشن
این کدی هست که بوسیله اون میتونید ip Local خودتون به همراه Subnet mask و Getway تغییر بدین
1- Add a reference to the System.Managemen
2- و اونو ایمپورت کنید Imports System.Management
3-


Private Sub SetIP(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()
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)
Catch ex As Exception

MessageBox.Show("An error occured: " + ex.Message)

End Try

Next

4- و نهایتا با
SetIP("192.168.1.230", "255.255.255.0", "192.168.1.51") صداش میزنین