Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey guys, does anybody know how to run an application as admin on form_load without editing the app.manifest? I really need this for my new project. Thanks :)

What I have tried:

Dim startInfo As New ProcessStartInfo()
       Dim myprocess As New Process()
       startInfo.FileName = "C:\test.exe"
       startInfo.Verb = "runas"
       startInfo.Arguments = "/env /user:" + "Administrator" + " cmd"
       myprocess.StartInfo = startInfo

This source code makes an error when I close the "Run as administrator" box, and when I copy this code to Form_Load, it just makes unlimited boxes of "Run as Administrator".
Updated 20-Jul-17 4:01am
Afzaal Ahmad Zeeshan 20-Jul-17 10:38am    
You need to get the admin permissions for your form, instead of the processes internally. What are you using in the form itself?
[no name] 20-Jul-17 10:47am    
What do you mean? I just want the application to ask for admin permissions
Afzaal Ahmad Zeeshan 20-Jul-17 10:51am    
Yes, you do. But in the code about you are actually asking for these permissions for a common process, which runs "test.exe". So I am asking what is the code that you are using in your WinForm? Can you show that?
Richard MacCutchan 20-Jul-17 13:19pm    
Just mark your application as "Run as administrator", rather than trying to change things at run time. However, you can only do this if you have access to an administrator account. So if you are trying to hack someone's system you will not be able to do it.
[no name] 6-Jul-18 11:49am    
Your code doesn't ask for permissions, it happens before your program runs. The manifest tells User Account Control whether it needs elevated permissions or not. The reason why you're getting an infinite loop is because the program starts, then starts itself while requesting admin permissions, then that one does the same thing etc.

If you must have the program itself requesting admin permissions, you'll have to have Program A using this code to run Program B with admin permissions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900