【C#】如何激活UAC提示,使得程序获得管理员权限。

D_zjf 2010-04-27 09:05:47
进行注册表操作时,会被UAC拦住抛出异常 拒绝访问注册表。操作无法继续。
如何激活UAC提示,用户允许后获得管理员权限呢~?

谢谢
...全文
801 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Justin-Liu 2010-04-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
权限问题
Manifest嵌入
http://topic.csdn.net/u/20091202/11/d0de6a0b-1f14-45ff-a5dd-3d31f93bd29b.html
[/Quote]
对 manifest
miaodh 2010-04-28
  • 打赏
  • 举报
回复
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
}
}


}
}
yufenghua 2010-04-27
  • 打赏
  • 举报
回复
使用manifest的方法解决了。
可以到网上搜索一下这个manifest
高版本的vs可以在linker->manifest里面找到一个默认为invoke的 改成requestAdministrator(以上可能有拼写错误)
引用他人的回复。。。。。
wuyq11 2010-04-27
  • 打赏
  • 举报
回复
weberwong 2010-04-27
  • 打赏
  • 举报
回复
右键,run as administrator就行了吧

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧