Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 897
Marco Avatar asked Mar 13 '13 18:03

Marco


People also ask

How do I force my .NET application to run as administrator?

Right click your executable, go to Properties > Compatibility and check the 'Run this program as admin' box.

How to run visual studio project as administrator?

Run Visual Studio as an administratorOpen the Start menu, and scroll to Visual Studio. From the right-click or context menu of Visual Studio 2019 or Visual Studio 2022, select More > Run as administrator. When Visual Studio starts, (Administrator) appears after the product name in the title bar.

How do I create a Windows console application?

Create the appOn the start page, choose Create a new project. On the Create a new project page, enter console in the search box. Next, choose C# or Visual Basic from the language list, and then choose All platforms from the platform list. Choose the Console Application template, and then choose Next.


3 Answers

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

like image 64
Alexey Avatar answered Sep 22 '22 08:09

Alexey


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.

like image 30
Kate Gregory Avatar answered Sep 19 '22 08:09

Kate Gregory


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?

like image 43
AnthonyLambert Avatar answered Sep 21 '22 08:09

AnthonyLambert