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>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
//獲得當(dāng)前登錄的Windows用戶標(biāo)示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判斷當(dāng)前登錄用戶是否為管理員
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();
//設(shè)置運行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//設(shè)置啟動動作,確保以管理員身份運行
startInfo.Verb = "runas";
//如果不是管理員,則啟動UAC
System.Diagnostics.Process.Start(startInfo);
//退出
System.Windows.Forms.Application.Exit();
}
}
}
}
相關(guān)文章
C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法
這篇文章主要介紹了C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法,涉及C#使用linq進(jìn)行查詢的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

