C sharp exe should ask for the prompt “run as administrator” when opening

I have an exe generated by a sharp program. when I run exe, I want the UAC prompt to be able to run exe as an administrator. I saw exe startup examples as admin by default. But how do I enable UAC so that it asks me to run exe as an administrator? Any idea?

+7
source share
4 answers

Windows vista / 7 / server 2008 R2 uses User Account Control (UAC) technology to protect and protect the OS from malware, limiting application privileges to administrator permission. It is for this reason that we need to make our application work as an administrator. This is usually done when the application starts. This process, right-click on the application and click "Run as administrator" to start the application with administrator rights. If we assume that the user forgot to do this and starts the application normally, then unexpected behavior may be observed. Since all actions requiring administrative privileges in win vista and higher (for example, calling a process, using the system disk, etc.) will not be performed.

To avoid running the application as an administrator each time, we can make our application run as the default administrator. To achieve this, we need to adjust a bit with the application manifest. An application phenomenon is an XML file that describes an application.

Follow these steps to get your .net application running as an administrator:

Step 1: go to the project properties and click the "View Windows Settings" button. This will open the file "app.manifest".

Step 2: In the requested “ExecutionLevel” key, change the value of the “requireAdministrator” level and the uiAccess value to “False”. A configuration level of "requireAdministrator" means that the application runs only for administrators and requires that the application be run using an administrator access token. Setting uiAccess as "False" means that the application does not need to enter input into the user interface of another window on the desktop. Applications that do not provide accessibility should set this flag to false. Applications that are required to enter input into other windows on the desktop (for example, on-screen keyboard) must be true.

Step 3. Save the changes, rebuild the application and install it in Win Vista or higher.

Now the application will start automatically as an administrator.

+4
source

in the visual studio, add the “Application manifest file” (if you don’t have one), then add <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> to it to request administrator privileges when starting the application.

+6
source

You need to add the "Application manifest file". Step by step

  • Right click on your project
  • Add New Item
  • Select "Application manifest file"
  • Find it
  • change level = "requireAdministrator"
+6
source

To achieve this, you can use the ProcessStartInfo class or the Windows OS .

You can also paste the manifest file into your .exe application and set the requestedExecutionLevel property.

There is also a way to enable / disable UAC through the Windows registry .

+1
source

All Articles