This article is a mirror article of machine translation, please click here to jump to the original article.

View: 36267|Reply: 3

[WinForm] C# programs run with administrator privileges

[Copy link]
Posted on 10/9/2016 9:14:38 AM | | | |
In Vista and Windows 7 and later, a security mechanism called UAC (User Account Control) has been added, and if UAC is turned on, even if the user logs in with administrator privileges, their applications cannot write to the system directory, system registry, and other settings that may affect the normal operation of the system by default. This mechanism greatly enhances the security of the system, but for application developers, we cannot force users to turn off UAC, but sometimes the application we develop needs to run in the way of administrator, how to achieve such a function?
Here's how a C# program implements prompting the user to run with administrator privileges.
In this example, the WinForm program demonstrates that a new project is generated and modified accordingly:
Method 1: Start via System.Diagnostics.Process.Start():
Implementation method: Modify the program file generated by default, and the modified code is as follows:
Since the code has already been commented on, it will not go into details;
Effect: Since it is started by calling the external call of System.Diagnostics.Process.Start(), when running directly through VS, it will not prompt VS to need administrator rights, only the program itself needs administrator rights, which is different from the program that generates the application. This is the main difference from the implementation of method 2.
Method 2: By adding an application manifest file:
On the Project Add New Item, select Application Manifest File and click the Add button
Once added, the app.manifest file is opened by default, which will:
Modified to:
Then open the Project Properties and modify the Manifest in the Resources in the Applications tab to the new app.manifest.
Rebuild the project, and when you open the program again, you will be prompted to run with administrator privileges.
Note that if you start debugging in VS, you will be prompted that this task requires the application to have elevated privileges. As shown below:
Select Restart with other credentials to do so.
Method 3: Modify the properties of the program file directly
Right-click on the program file in the Compatibility tab in the Properties dialog box that pops up
Check "Run this program as administrator".







Previous:{"Element '_id' does not match any field or property of class."}
Next:.NET/C# 4 Ways to Implement MongoDB Queries and Fuzzy Queries [with Source Code]
 Landlord| Posted on 10/9/2016 9:15:27 AM |
Determine whether the program is running as an administrator
Namespaces need to be added:
using System.Security.Principal;


 Landlord| Posted on 1/26/2021 9:59:52 AM |
C# determines whether a program is running as an administrator
https://www.itsvse.com/thread-3004-1-1.html
 Landlord| Posted on 10/13/2025 9:18:33 AM |
.NET Core apps run as administrators on Windows systems, the code is as follows:


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com