How do I make a console app always run as an administrator?

28,222

Solution 1

First, understand that there WILL BE a UAC dialog at some point in this process. There is no way to avoid it. There are a few approaches you can take:

  • Have the people run the ERP software elevated. I am including this only for completeness. It is awful to have to consent to the UAC prompt every time you run the app when you usually don't need the powers. It would be a handy quick test, though, for you to confirm that if your app is elevated, things will work. Everything launched from an elevated process is elevated, so if your app still gets the error message, this isn't something you'll fix by elevating.
  • Change the code in the ERP app to launch your app elevated. You mention C#. Launching with the runas verb is an approach here. This puts the burden on the ERP developer and they may not be able to do it.
  • Add a manifest to your app. You can embed one, or if your app is called foo.exe just hand-create a foo.exe.manifest file with the appropriate requestedExecutionLevel. To embed one, use the Properties page of your C# project to choose the right kind of manifest. Make sure you're launched with UseShellExecute set to true, or the manifest will be ignored.

If you choose the first option, the UAC prompt will be every time the ERP app launches. Not good. If you choose the second or third, the UAC prompt will be every time your console app is launched from the ERP app. Probably acceptable.

One other thing you might consider is looking far more seriously into why the console app needs admin privs. Are you writing to the root of C or to Program Files? Are you updating a registry key in HKLM? Whatever you're doing, why are you doing it that way? Unless your app installs or configures something (in which case getting a UAC prompt is good and proper) you should try to adapt it so that it writes to pre-user storage and doesn't need elevation. Then you will no longer be worrying about how to launch it elevated, in any case.

Solution 2

Add into your project Application Manifest File (Add -> New Item -> General -> Application Manifest File) and add the below node into the app.manifest:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

http://msdn.microsoft.com/en-us/library/windows/desktop/bb756929.aspx

Solution 3

create a batch file containing something like:

runas /env /user:%USERDOMAIN%\%USERNAME% cmd.exe /K YourProgramName.exe

Where %USERDOMAIN% and %USERNAME% are replaced by your admin account details.

And run that instead?

Share:
28,222
Marco
Author by

Marco

Updated on August 14, 2020

Comments

  • Marco
    Marco over 3 years

    I have a console application that was developed to be called by a erp software.

    They call my app inside the erp and when they do it, i always get errors related to the insufficient permission to do it.

    I have checked the "run this program as an administrator" checkbox in the properties of the exe for all users but the result is the same.

    I have read something about adding a manifest that will make the app prompt for the uac dialog, but thats not what i want because the app will be called from erp on the server and clients will not see the dialog on server.

    Can someone explain me how to make this console app always run as administrator?