内容里的说明够详细了吧,只要把 asInvoker替换成requireAdministrator,我们的程序就会默认要求管理员权限运行了
下面再说下怎么给程序的按钮上也加上小盾牌图标吧
这我们就需要调用Win32 API了
要调用API么,要先引用命名空间
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
///////////////////////////////////////////////////////////////////////
///
/// Enables the elevated shield icon on the given button control
///
///
/// Button control to enable the elevated shield icon on.
///
///////////////////////////////////////////////////////////////////////
private void EnableElevateIcon_BCM_SETSHIELD(Button ThisButton)
{
// Input validation, validate that ThisControl is not null
if (ThisButton == null)
{
return;
}
// Define BCM_SETSHIELD locally, declared originally in Commctrl.h
uint BCM_SETSHIELD = 0x0000160C;
// Set button style to the system style
ThisButton.FlatStyle = FlatStyle.System;
// Send the BCM_SETSHIELD message to the button control
SendMessage(new HandleRef(ThisButton, ThisButton.Handle), BCM_SETSHIELD, new IntPtr(0), new IntPtr(1));
} 运行效果: 参考资料:
UAC可以在编译时设置,提示用户输入管理员密码才能执行,参考http://msforums.ph/forums/t/52208.aspx
如果嫌这个麻烦,可以跳过UAC检查,参考http://www.agileit.com/Blog/Lists/Posts/Post.aspx?ID=265 http://forum.sysinternals.com/topic13776.html
选择性禁止UAC,参考http://www.techrepublic.com/blog/window-on-windows/selectively-disable-uac-for-your-trusted-vista-applications/635