0

enter image description here

how to change this two values in visual basi.net. I do not want to use vbscript to do this.

i searched and get this - How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#

^ Converted code:

''' <summary>
''' Set's a new IP Address and it's Submask of the local machine
''' </summary>
''' <param name="ip_address">The IP Address</param>
''' <param name="subnet_mask">The Submask IP Address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setIP(ByVal ip_address As String, ByVal subnet_mask As String)

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

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            Try
                Dim setIP As ManagementBaseObject
                Dim newIP As ManagementBaseObject = objMO.GetMethodParameters("EnableStatic")

                newIP("IPAddress") = New String() {ip_address}
                newIP("SubnetMask") = New String() {subnet_mask}

                setIP = objMO.InvokeMethod("EnableStatic", newIP, Nothing)
            Catch generatedExceptionName As Exception
                Throw


            End Try
        End If
    Next
End Sub
''' <summary>
''' Set's a new Gateway address of the local machine
''' </summary>
''' <param name="gateway">The Gateway IP Address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setGateway(ByVal gateway As String)
    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            Try
                Dim setGateway As ManagementBaseObject
                Dim newGateway As ManagementBaseObject = objMO.GetMethodParameters("SetGateways")

                newGateway("DefaultIPGateway") = New String() {gateway}
                newGateway("GatewayCostMetric") = New Integer() {1}

                setGateway = objMO.InvokeMethod("SetGateways", newGateway, Nothing)
            Catch generatedExceptionName As Exception
                Throw
            End Try
        End If
    Next
End Sub
''' <summary>
''' Set's the DNS Server of the local machine
''' </summary>
''' <param name="NIC">NIC address</param>
''' <param name="DNS">DNS server address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setDNS(ByVal NIC As String, ByVal DNS As String)
    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            ' if you are using the System.Net.NetworkInformation.NetworkInterface you'll need to change this line to if (objMO["Caption"].ToString().Contains(NIC)) and pass in the Description property instead of the name 
            If objMO("Caption").Equals(NIC) Then
                Try
                    Dim newDNS As ManagementBaseObject = objMO.GetMethodParameters("SetDNSServerSearchOrder")
                    newDNS("DNSServerSearchOrder") = DNS.Split(","c)
                    Dim setDNS As ManagementBaseObject = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, Nothing)
                Catch generatedExceptionName As Exception
                    Throw
                End Try
            End If
        End If
    Next
End Sub
''' <summary>
''' Set's WINS of the local machine
''' </summary>
''' <param name="NIC">NIC Address</param>
''' <param name="priWINS">Primary WINS server address</param>
''' <param name="secWINS">Secondary WINS server address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setWINS(ByVal NIC As String, ByVal priWINS As String, ByVal secWINS As String)
    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            If objMO("Caption").Equals(NIC) Then
                Try
                    Dim setWINS As ManagementBaseObject
                    Dim wins As ManagementBaseObject = objMO.GetMethodParameters("SetWINSServer")
                    wins.SetPropertyValue("WINSPrimaryServer", priWINS)
                    wins.SetPropertyValue("WINSSecondaryServer", secWINS)

                    setWINS = objMO.InvokeMethod("SetWINSServer", wins, Nothing)
                Catch generatedExceptionName As Exception
                    Throw
                End Try
            End If
        End If
    Next
End Sub

but gives error in ManagementClass() and other items. I imported System.Management. But vb shows error that it is not found.

This is code i converted to print nic available in a pc. Is it correct? :

    For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
        If nic.OperationalStatus = OperationalStatus.Up Then
            Debug.Print(nic.GetPhysicalAddress().ToString())
            Exit For
        End If
    Next

but was is nic name to supply? or a example demo to use it?

Community
  • 1
  • 1
Vpp Man
  • 2,384
  • 8
  • 43
  • 74

2 Answers2

1

You've already found some people who were nice enough to write most of the code for you, now all you have to do is translate it to VB.NET. That shouldn't be hard for any real .NET programmer, given the extensive similarities between the two languages. But if you need a little extra help, try one of the free online translators, like this one from Developer Fusion.

The only remaining issue, then, is how you know which NIC name to supply, since the linked code accepts a string parameter specifying the NIC.

Unfortunately, that's not a question to which we (or anyone) can give you an exact answer. The issue is that a single computer can have multiple NICs installed, and each of them can have different DNS settings. That's why the function requires the parameter so that you can choose which one to configure.

The simple, straightforward approach is to enumerate all of the installed NICs, then

  • if there is only one, use that one.
  • if there is more than one, display a dialog that lists the name of each and asks the user to choose.

Observe that the linked code uses the Caption property of the Win32_NetworkAdapterConfiguration class as the "name" of the NIC. This is probably the same name that you should display to the user, along with any of the other property values that you think might contain useful information to help them in making a decision.

Asking the user is almost always a cop-out approach, considering that the user does not care and most likely does not know the answer. Good UI design keeps stuff like this to a bare minimum. But here, you really do not have much choice. Rationalize the choice with the logic that if a user has more than one NIC installed on her computer, she is probably an advanced user who will know how to answer your question.

Modifying the code to enumerate all of the installed NICs is relatively straightforward. Starting from Marc's refactored code:

foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"] && objMO["Caption"].Equals(nic)))

we just need to remove the equality test applied to the Caption property:

foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"]))
Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • thnq. i added converted code. but vb showing Type not defined error for `Management*()` – Vpp Man Mar 15 '13 at 08:35
  • @VppMan Yes, as the `remarks` section of the XML documentation says, you need to add a project reference to the [`System.Management` namespace](http://msdn.microsoft.com/en-us/library/system.management.aspx). The code uses classes defined in this namespace, which encapsulate the WMI interface. – Cody Gray - on strike Mar 15 '13 at 08:36
  • tnqu Cody. i am going to test it. If it works, then i can see the new values in the dns addresses(marked portion on image above in my question). Correct? – Vpp Man Mar 15 '13 at 08:46
1

You need to reference System.management as well as import it.

Nick
  • 11
  • 1