none
Start a process from vb with administrator rights? RRS feed

  • Question

  • Hi,

    I'm trying to start a different application from my  vb project, I'm using this code:

    Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop & "\programtostart.exe")

    This works perfectly, but I need the application to be started with administrator rights, how can I do this?

    Its because its an install program.

    I also need it to be functional on XP, Vista and 7.

    Thanks!
    Saturday, January 30, 2010 12:39 AM

Answers

  • Try this
    Dim process As System.Diagnostics.Process = Nothing
    Dim processStartInfo As System.Diagnostics.ProcessStartInfo
    processStartInfo = New System.Diagnostics.ProcessStartInfo()
    processStartInfo.FileName = My.Computer.FileSystem.SpecialDirectories.Desktop & "\programtostart.exe"
    processStartInfo.Verb = "runas"
     processStartInfo.Arguments = "" processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal processStartInfo.UseShellExecute = True process = System.Diagnostics.Process.Start(processStartInfo)
    kaymaf

    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/.

    • Marked as answer by live410 Thursday, February 4, 2010 12:03 PM
    Saturday, January 30, 2010 1:31 AM

All replies

  • Try this
    Dim process As System.Diagnostics.Process = Nothing
    Dim processStartInfo As System.Diagnostics.ProcessStartInfo
    processStartInfo = New System.Diagnostics.ProcessStartInfo()
    processStartInfo.FileName = My.Computer.FileSystem.SpecialDirectories.Desktop & "\programtostart.exe"
    processStartInfo.Verb = "runas"
     processStartInfo.Arguments = "" processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal processStartInfo.UseShellExecute = True process = System.Diagnostics.Process.Start(processStartInfo)
    kaymaf

    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/.

    • Marked as answer by live410 Thursday, February 4, 2010 12:03 PM
    Saturday, January 30, 2010 1:31 AM
  • Thanks kaymaf,


    I haven't tested that code yet, but how does it work? Which part refers to adminstrator rights? :)
    Saturday, January 30, 2010 8:33 AM
  • The line below is the same as when you right click your program from windows explorer and click Run as Administrator

    processStartInfo.Verb = "runas"


    kaymaf

    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/.

    Saturday, January 30, 2010 9:42 AM
  • Thanks! That sounds great!

    Just one more thing, how do I check that the process is running with admin rights?

    Is there any way I can tell?
    Saturday, January 30, 2010 12:07 PM
  • Thanks! That sounds great!

    Just one more thing, how do I check that the process is running with admin rights?

    Is there any way I can tell?


     Im not sure if there is way to detect a particular process running with administrator rights but you can check if the current user is a administrator or a member of administrators group. This link have sample code http://blogs.msdn.com/jaredpar/archive/2007/08/01/detecting-if-you-are-an-admin.aspx

    kaymaf

    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/ .

    Saturday, January 30, 2010 3:47 PM
  • Hi live410,

    Please try the sample codes in the following link to check whether the process is running with admin right:
    http://www.dreamincode.net/code/snippet1577.htm 

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, February 1, 2010 10:30 AM
    Moderator
  • http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/053a0a87-e9b8-42e7-b483-d6d47c41278e
    Success
    Cor

    what this link have to do with OP question. I noticed that if you dont have answer or understand a question you always assuming that the question is illegal on this forum.

    Dont judge people based on question they asked.

    kaymaf
    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/.

    Monday, February 1, 2010 6:29 PM
  • http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/053a0a87-e9b8-42e7-b483-d6d47c41278e
    Success
    Cor

    what this link have to do with OP question. I noticed that if you dont have answer or understand a question you always assuming that the question is illegal on this forum.

    Dont judge people based on question they asked.

    kaymaf

    Why become so overhitted when I show a link which warns us to keep an eye on malicious code.

    Do yo feel guilty somehow?

    When I don't understand a question, I don't reply on it.

    Mostly I mark than an answer as helpful if there is an answer that makes it me understand.




    Success
    Cor
    Tuesday, February 2, 2010 12:00 PM

  • Why become so overhitted when I show a link which warns us to keep an eye on malicious code.

    Do yo feel guilty somehow?

    When I don't understand a question, I don't reply on it.

    Mostly I mark than an answer as helpful if there is an answer that makes it me understand.




    Success
    Cor

    Is OP ever asked for malicious code? The answer is NO. So your ignorant assumption is dead wrong, I have seen this kind of question in many forums on the web, what i can tell you is that, if you don't understand a question, just back off the thread because judging someone based on a question that you have no clue about is an ignorant.

    kaymaf

    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/.

    Tuesday, February 2, 2010 5:11 PM
  • Is OP ever asked for malicious code? The answer is NO.

    kaymaf



    ROFL
    Success
    Cor
    Tuesday, February 2, 2010 7:07 PM
  • Thankyou kaymaf. you're exactly right :)

    Cor seems to have a habit of posting unhelpful replies, at least what I've seen anyway.

    Thanks for the help!
    Thursday, February 4, 2010 12:02 PM
  • I use this code to install services. I check the OS version before use the "runas"

    Dim RunSvc As New System.Diagnostics.ProcessStartInfo(PathFile)
    RunSvc.Arguments = "-install"
    RunSvc.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
    If System.Environment.OSVersion.Version.Major >= 6 Then RunSvc.Verb = "runas"
    Dim ServiceProcess As Process = System.Diagnostics.Process.Start(RunSvc)
    Do Until ServiceProcess.HasExited = True
             Me.UseWaitCursor = True
             System.Threading.Thread.Sleep(1000)
             Me.lblSvcStatus.Text = "Installing..."
             Application.DoEvents()
    Loop

    Tuesday, May 25, 2010 3:18 PM