C#中用管理員身份運行程序代碼實例
更新時間:2015年02月26日 10:42:56 投稿:junjie
這篇文章主要介紹了C#中用管理員身份運行程序代碼實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
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 { //創(chuàng)建啟動對象 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(); } } } }
相關文章
UnityShader使用Plane實現(xiàn)翻書效果
這篇文章主要為大家詳細介紹了UnityShader使用Plane實現(xiàn)翻書效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07深入分析緩存依賴中cachedependency對象及周邊小講
本篇文章是對緩存依賴中cachedependency對象進行了詳細的分析介紹,需要的朋友參考下2013-06-06WinForm實現(xiàn)攔截窗體上各個部位的點擊特效實例
這篇文章主要介紹了WinForm實現(xiàn)攔截窗體上各個部位的點擊特效實例,對窗體上各個部位進行定義,從而實現(xiàn)了點擊特效,需要的朋友可以參考下2014-09-09如何利用C#通過sql語句操作Sqlserver數(shù)據(jù)庫教程
ado.net提供了豐富的數(shù)據(jù)庫操作,下面這篇文章主要給大家介紹了關于如何利用C#通過sql語句操作Sqlserver數(shù)據(jù)庫教程的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-10-10