using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
namespace UACApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GetAndDisplayRights();
}
private void GetAndDisplayRights()
{
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
lblRights.Text = hasAdministrativeRight ? "Running with full rights" : "Running with limited rights";
}
private void btnElevate_Click(object sender, EventArgs e)//提升本程序权限重新运行
{
RunElevated(Application.ExecutablePath);
//Close this instance because we have an elevated instance
this.Close();
}
private void btnNotepad_Click(object sender, EventArgs e) //提升其他程序权限运行
{
//Must run with limited privileges in order to see the UAC window
RunElevated("notepad.exe");
}
private void RunElevated(string fileName)
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = fileName;
try
{
Process.Start(processInfo);
}
catch (Win32Exception)
{
//Do nothing. Probably the user canceled the UAC window
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)