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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.