Run a NETSH command through code

I have the following command line:

netsh interface ip set dns "optus" static 208.67.222.222 primary

I use it to change the default dns server after i connect to my isp.

Is there a way I can do this programatically? is there an api call that does this function without calling NETSH??
LVL 1
Andy1Asked:
Who is Participating?
 
Dennis AriesCEO @ Arkro ITCommented:
You can make use of the ManagementClass to change this kind of information. Add a reference to the System.Management namespace and use the code below.
 

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 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

Open in new window

0
 
Andy1Author Commented:
ok, i added:

Imports System.Management

i now  have 2 errors:

Error      1      'NIC' is not declared. It may be inaccessible due to its protection level.
Error      2      'DNS' is not declared. It may be inaccessible due to its protection level.

any ideas?
0
 
Andy1Author Commented:
opps my bad, ignore that
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Dennis AriesCEO @ Arkro ITCommented:
*grin*
0
 
Andy1Author Commented:
Its not working.. no errors, just doesn't change the dns of the connection...
0
 
Andy1Author Commented:
ok, i think i've worked out why its not working.

I'm trying to change the DNS of my 3G wireless adaptor. Is it not working becuse its a PPP adaptor?
0
 
Andy1Author Commented:
Any ideas on this?
0
 
Dennis AriesCEO @ Arkro ITCommented:
Not sure. I've only changed the information of physical adapters. I'll try to look into it in the next few days.
0
 
Andy1Author Commented:
cheers, i'll let you know if i find anything more.
0
 
CodeCruiserCommented:
You can execute the command using process.start

Follow the example(Launch Invisible Processes) in this link

http://www.devx.com/dotnet/Article/7914/1954


also
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ShellCommandsInCS12032005042031AM/ShellCommandsInCS.aspx

0
 
Dennis AriesCEO @ Arkro ITCommented:
I've worked on your problem and the only moment that the DNS has not been changed is when I did not have sufficient rights to set the DNS.
Just to be sure, do you run your program as administrator ('run as administrator' in Windows Vista and Windows 7)?
An alternative to this run-as is to add the required privilege to your app.config.
0
 
Andy1Author Commented:
UAC is turned off, the user account has full admin privilages.

I added a line:

debug.print objMO("Caption").

after the FOR statement. It only listed my internal wireless card adaptor, it didn't see my 3G connection as a network interface, i am assuming that its possibly because its a 3G USB modem that connects via a dialup connection?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.