C#中用管理員身份運(yùn)行程序代碼實(shí)例
更新時(shí)間:2015年02月26日 10:42:56 投稿:junjie
這篇文章主要介紹了C#中用管理員身份運(yùn)行程序代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyWebBrowser
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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))
{
//如果是管理員,則直接運(yùn)行
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
//創(chuàng)建啟動(dòng)對(duì)象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//設(shè)置運(yùn)行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//設(shè)置啟動(dòng)動(dòng)作,確保以管理員身份運(yùn)行
startInfo.Verb = "runas";
//如果不是管理員,則啟動(dòng)UAC
System.Diagnostics.Process.Start(startInfo);
//退出
System.Windows.Forms.Application.Exit();
}
}
}
}
相關(guān)文章
UnityShader使用Plane實(shí)現(xiàn)翻書效果
這篇文章主要為大家詳細(xì)介紹了UnityShader使用Plane實(shí)現(xiàn)翻書效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
深入分析緩存依賴中cachedependency對(duì)象及周邊小講
本篇文章是對(duì)緩存依賴中cachedependency對(duì)象進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
WinForm實(shí)現(xiàn)攔截窗體上各個(gè)部位的點(diǎn)擊特效實(shí)例
這篇文章主要介紹了WinForm實(shí)現(xiàn)攔截窗體上各個(gè)部位的點(diǎn)擊特效實(shí)例,對(duì)窗體上各個(gè)部位進(jìn)行定義,從而實(shí)現(xiàn)了點(diǎn)擊特效,需要的朋友可以參考下2014-09-09
如何利用C#通過sql語句操作Sqlserver數(shù)據(jù)庫教程
ado.net提供了豐富的數(shù)據(jù)庫操作,下面這篇文章主要給大家介紹了關(guān)于如何利用C#通過sql語句操作Sqlserver數(shù)據(jù)庫教程的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

