Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > How i can change ip , mask and g/w?
 

How i can change ip , mask and g/w?

How i can change ip , mask and  g/w with VB command?
or read this parameters&
Gadzhi  Thursday, October 15, 2009 8:39 AM
i know comand netsh from shelll cmd
Gadzhi  Thursday, October 15, 2009 9:09 AM

Hi Gadzhi,

Welcometo MSDN forums!

This article demonstrates the power of WMI, on how to configure TCP/IP Setting programmatically using C#.NET.
http://www.codeproject.com/KB/system/cstcpipwmi.aspx

I posted the corresponding VB.NET code sample here for you to check: How to change IP address, SubnetMask, Gateway via WMI feature?

To use WMI in VB.NET, beforehand you need to add reference System.Management.dll assemblyto your project.
Project menu -> Add Reference -> .NET tab -> Find System.Management and add it.

Imports System.Management

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim IPAddress As String = "192.168.0.10"

Dim SubnetMask As String = "255.255.255.0"

Dim Gateway As String = "192.168.0.1"

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)

Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")

Catch ex As Exception

MessageBox.Show("Unable to Set IP : " & ex.Message)

End Try

Next objMO

End Sub

End Class

Check this thread for reference:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/3ca76e36-a737-4486-835d-874f71b22dc5


Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Monday, October 19, 2009 10:46 AM

You can use google to search for other answers

Custom Search

More Threads

• Patrice Pelland's 'Microsoft Visual Basic 2005 Express Edition: Build a Program Now! - Where are the Icons?
• F1 (help)
• Deleting files - Asses to the path is denied
• Textbox text to change when webbrowser page changes
• datagridview help
• vb2008 file transferring trought an socket.
• i need some help :(
• combobox help please
• How do I change the primary key of my SQL data base?
• Matching Texts