반응형

* VBNET WMI 를 이용한 네트워크 IP 및 Subnet, Gateway 설정 하기 예제...

- WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 imports System.Management

 

Main

전체 소스 코드

Form1.vb

 

Imports System.Management

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

        For Each MO As ManagementObject In objMOC

            If Not CType(MO("IPEnabled"), Boolean) Then
                Continue For
            End If

            Try
                Dim objNewIP As ManagementBaseObject = Nothing
                Dim objSetIP As ManagementBaseObject = Nothing
                Dim objNewGate As ManagementBaseObject = Nothing
                objNewIP = MO.GetMethodParameters("EnableStatic")
                objNewGate = MO.GetMethodParameters("SetGateways")

                'Set Gateway
                objNewGate("DefaultIPGateway") = New String() {txtGateway.Text}
                objNewGate("GatewayCostMetric") = New Integer() {1}
                'Set IP
                objNewIP("IPAddress") = New String() {txtIP.Text}
                'Set Subnet
                objNewIP("SubnetMask") = New String() {txtSubnet.Text}

                objSetIP = MO.InvokeMethod("EnableStatic", objNewIP, Nothing)
                objSetIP = MO.InvokeMethod("SetGateways", objNewGate, Nothing)

                objSetIP = MO.InvokeMethod("EnableStatic", objNewIP, Nothing)
                objSetIP = MO.InvokeMethod("SetGateways", objNewGate, Nothing)

                MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway!")
            Catch ex As Exception
                MessageBox.Show("Unable to Set IP : " + ex.Message)
            End Try

        Next


    End Sub

End Class

*예제 결과

 

윈도우 시작 -> 실행 -> cmd -> ipconfig 로 네트워크 IP 확인 가능

 

변경하기 전 IP 

 



변경 후 네트워크 IP

 

 

https://kdsoft-zeros.tistory.com/155

 

[VBNET] [WMI] 네트워크 IP 및 Subnet, Gateway 얻어오기

*VBNET WMI 를 이용한 네트워크 IP 및 Subnet, Gateway 얻어 오기 예제... 전체 소스 코드 Form1.vb Imports System.Management Public Class Form1 Private Sub button1_Click(ByVal sender As System.Object, B..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts