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?
Right click your executable, go to Properties > Compatibility and check the 'Run this program as admin' box.
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.
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.
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
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:
runas
verb is an approach here. This puts the burden on the ERP developer and they may not be able to do it.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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With