C#中用管理員身份運行程序代碼實例
阿新 • • 發佈:2017-12-14
window bottom windows sum etc ans left rec 跑步 除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#中用管理員身份運行程序代碼實例
本文地址: http://www.paobuke.com/develop/c-develop/pbk23127.html
C#實現判斷當前操作用戶管理角色的方法C#特性之匿名方法和Lambda表達式C#省份城市下拉框聯動簡單實現方法C#調用SQL語句時乘號的用法
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace MyWebBrowser { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { //獲得當前登錄的Windows用戶標示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判斷當前登錄用戶是否為管理員 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { //如果是管理員,則直接運行 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { //創建啟動對象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); //設置運行文件 startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; //設置啟動動作,確保以管理員身份運行 startInfo.Verb = "runas"; //如果不是管理員,則啟動UAC System.Diagnostics.Process.Start(startInfo); //退出 System.Windows.Forms.Application.Exit(); } } } }
C#中用管理員身份運行程序代碼實例
本文地址: http://www.paobuke.com/develop/c-develop/pbk23127.html
相關內容
C#深度優先搜索算法C#實現JSON字符串序列化與反序列化的方法深入解析C#中的abstract抽象類C#繪制曲線圖的方法C#實現判斷當前操作用戶管理角色的方法C#特性之匿名方法和Lambda表達式C#省份城市下拉框聯動簡單實現方法C#調用SQL語句時乘號的用法
C#中用管理員身份運行程序代碼實例