Results 1 to 4 of 4

Thread: Process doesnt start as admin / Execute CMD in Background as admin

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2024
    Posts
    2

    Question Process doesnt start as admin / Execute CMD in Background as admin

    Hello, here is a small code snippet. I would like to change the second DNS entry on the network adapter with this code. But when I run the program, nothing happens. The syntax is ok. When I start a CMD and run the commands, I need administrator rights. If I run the CMD as admin in the command line, the command also works. The logged in user is also an administrator. Even if I run the program as administrator, the CMD in the background doesn't work as admin.

    Now my question is, how can I execute the command within my VB.NET program without having to specify "run as.."? As I said, the user is an administrator.

    Can anyone here help me solve the problem? Thanks.

    Code:
    Dim p1 As New Process()
    Dim psi1 As New ProcessStartInfo("netsh", "interface ip add dns " & Chr(34) & cAdapter.Text & Chr(34) & " " & DNS2 & " index=2")
    p1.StartInfo = psi1
    p1.StartInfo.UseShellExecute = False
    p1.StartInfo.RedirectStandardOutput = True
    p1.Start()
    p1.WaitForExit()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,596

    Re: Process doesnt start as admin / Execute CMD in Background as admin

    For the record, this:
    Code:
    "interface ip add dns " & Chr(34) & cAdapter.Text & Chr(34) & " " & DNS2 & " index=2"
    would be better written like this:
    Code:
    $"interface ip add dns ""{cAdapter.Text}"" {DNS2} index=2"
    Much more readable and therefore less error-prone. I would recommend never using more than a single concatenation operator together - always use string interpolation or some other option if you need more than one. I would also recommend using literal double-quotes rather than Chr(34) because it's more readable and doesn't require you to translate in your head.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,596

    Re: Process doesnt start as admin / Execute CMD in Background as admin

    I just searched the web for ".net process.start as administrator" and the very first result seems to address your issue. We can always help with the stuff you can't work out but you should always at least make an effort on your own behalf first, which would include a basic web search.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,577

    Re: Process doesnt start as admin / Execute CMD in Background as admin

    As jmcilhinney told you, you should always do some research before asking for help…

    Code:
    Dim p1 As New Process()
    Dim psi1 As New ProcessStartInfo("netsh", "interface ip add dns " & Chr(34) & cAdapter.Text & Chr(34) & " " & DNS2 & " index=2")
    p1.StartInfo = psi1
    p1.StartInfo.UseShellExecute = False
    p1.StartInfo.RedirectStandardOutput = True
    p1.StartInfo.Verb = “runas”
    p1.Start()
    p1.WaitForExit()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width