Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report