In Winform, the method is started as Administrator by default.

Source: Internet
Author: User

We know thatWindows Vista"UAC" permission control is added to Microsoft operating systems. This is a significant improvement in security performance.
In later systems, the software runs as a common user by default, unless the software Declaration requires the Administrator to run, or the user is forced to run as an administrator.
When we perform some sensitive functions, such as killing processes, reading and writing memory, changing system files, and changing system settings, we need to provide administrator permissions. Otherwise, we cannot obtain objects. In this case, we need to manually declare that the program requires the administrator privilege to run. So how can we do it?
Here, Microsoft MSDN is quoted as saying:

Right-click your project and create a mainfest Application List) file,
<! -- UAC configuration options
If you want to change the Windows User Account Control level, replace it with one of the following nodes:
RequestedExecutionLevel node.
<RequestedExecutionLevel level = "asInvoker" uiAccess = "false"/>
<RequestedExecutionLevel level = "requireAdministrator" uiAccess = "false"/>
<RequestedExecutionLevel level = "highestAvailable" uiAccess = "false"/>
Specifying a requestedExecutionLevel node will disable file and Registry virtualization.
If you want to use file and Registry virtualization to achieve backward
The requestedExecutionLevel node is deleted.
-->
Set <requestedExecutionLevel = "asInvoker" uiAccess = "false"/> to requireAdministrator and save and run the command. When the program requires administrator permission, the system will prompt whether the user is allowed.

This is a method. Another method is often used. This should be written in Program. cs.

 1: static void Main(string[] Args)
 2: {
 3: /**
4: * when the current user is an administrator, start the application directly.
5: * if it is not an administrator, use the startup object to start the program to ensure that it runs as an administrator.
 6: */
7: // obtain the Windows User ID for the current Logon
 8: System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
9: // create a Windows user topic
 10: Application.EnableVisualStyles();
 11: 
 12: System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
13: // determine whether the current logon user is an administrator
 14: if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
 15: {
16: // run
 17: 
 18: Application.EnableVisualStyles();
 19: Application.Run(new Form1());
 20: }
 21: else
 22: {
23: // create a startup object
 24: System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
25: // set the running File
 26: startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
27: // set startup parameters
 28: startInfo.Arguments = String.Join(" ", Args);
29: // set the startup action to run as an administrator
 30: startInfo.Verb = "runas";
31: // if not the administrator, start UAC
 32: System.Diagnostics.Process.Start(startInfo);
33: // exit
 34: System.Windows.Forms.Application.Exit();
 35: }
 36: }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.