Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add code that runs by default as an administrator in the program?

Shulou Source: shulou.com Published: 2022-06-03 15:48:37 09月25日 Update

This article mainly explains "how to add the code that runs by default as an administrator in the program". The explanation in the article is simple and clear and easy to learn and understand. let's study and learn "how to add code run by default as an administrator in the program". Let's study and learn how to add code run by default as an administrator in the program.

The program runs as an administrator by default

The copy code is as follows:

Static void Main (string [] Args)

{

/ * *

* launch the application directly when the current user is an administrator

* if not an administrator, start the program using the startup object to ensure that it runs as an administrator

, /

/ / obtain the Windows user ID of the current login

System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent ()

/ / create a Windows user topic

Application.EnableVisualStyles ()

System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal (identity)

/ / determine whether the currently logged-in user is an administrator

If (principal.IsInRole (System.Security.Principal.WindowsBuiltInRole.Administrator))

{

/ / if you are an administrator, run it directly

Application.EnableVisualStyles ()

Application.Run (new Form1 ())

}

Else

{

/ / create a startup object

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo ()

/ / set the running file

StartInfo.FileName = System.Windows.Forms.Application.ExecutablePath

/ / set startup parameters

StartInfo.Arguments = String.Join ("", Args)

/ / set the startup action to ensure that it runs as an administrator

StartInfo.Verb = "runas"

/ / start UAC if you are not an administrator

System.Diagnostics.Process.Start (startInfo)

/ / exit

System.Windows.Forms.Application.Exit ()

}

}

Open the Program.cs file in the assembly and replace the code in the Main method with the above code to realize that the program runs as an administrator by default.

Thank you for your reading, the above is the content of "how to add the code run by default to the administrator in the program". After the study of this article, I believe you have a deeper understanding of how to add the code run by default to the administrator in the program, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Administrator Administration run Program Code Category China plus user identity Learning content object File Login theme Action Parameter that is Application idea Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Apple macOS Docker