我有一个 WPF 应用程序,它可以访问本地计算机上的 Windows 服务、任务调度程序。当我部署此 WPF 应用程序并在没有“以管理员身份运行”的情况下运行它时,它会失败,因为它无法访问本地计算机上的 Windows 服务和任务调度程序。如果我使用“以管理员身份运行”运行它,它可以正常工作。
当我的应用程序部署在生产环境中时,如何让我的应用程序默认以管理员模式运行?
您需要添加一个app.manifest
. 将更改requestedExecutionLevel
为。您可以使用添加文件对话框创建新清单,将其更改为需要管理员。确保您的项目设置也设置为使用该清单。这将允许您简单地双击应用程序,如果还没有,它将自动提示提升。asInvoker
requireAdministrator
有关更多文档,请参见此处:
http://msdn.microsoft.com/en-us/library/bb756929.aspx
编辑:对于它的价值,这篇文章使用 VS 2005 并使用mt.exe
来嵌入清单。如果您使用的是 Visual Studio 2008+,这是内置的。只需打开项目的属性,然后在“应用程序”选项卡上选择清单。
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
到
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
然后 WPF 应用程序将以管理员身份运行。
使 WPF 应用程序以管理员模式运行的步骤
1.打开解决方案资源管理器
2.右键点击解决方案--->Add---->New Item---->App.Manifest---->OK
3.编辑Manifest文件如下:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
(到)
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
4.编辑Manifest文件后,转到解决方案项目(RightCLick)----->properties------->Security
勾选“启用 ClickOnce 安全设置”复选框
如果您不想破坏 Clickonce,则此代码是最佳解决方案:
using System.Security.Principal;
using System.Management;
using System.Diagnostics;
using System.Reflection;
//Put this code in the main entry point for the application
// Check if user is NOT admin
if (!IsRunningAsAdministrator())
{
// Setting up start info of the new process of the same application
ProcessStartInfo processStartInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().CodeBase);
// Using operating shell and setting the ProcessStartInfo.Verb to “runas” will let it run as admin
processStartInfo.UseShellExecute = true;
processStartInfo.Verb = "runas";
// Start the application as new process
Process.Start(processStartInfo);
// Shut down the current (old) process
System.Windows.Forms.Application.Exit();
}
}
/// <summary>
/// Function that check's if current user is in Aministrator role
/// </summary>
/// <returns></returns>
public static bool IsRunningAsAdministrator()
{
// Get current Windows user
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
// Get current Windows user principal
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
// Return TRUE if user is in role "Administrator"
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
WPF App.xaml.cs
当前应用程序进程将终止,并且以管理员身份运行的具有新进程的相同应用程序将启动。
public partial class App : Application
{
//This function will be called on startup of the applications
protected override void OnStartup(StartupEventArgs e)
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (principal.IsInRole(WindowsBuiltInRole.Administrator) == false && principal.IsInRole(WindowsBuiltInRole.User) == true)
{
ProcessStartInfo objProcessInfo = new ProcessStartInfo();
objProcessInfo.UseShellExecute = true;
objProcessInfo.FileName = Assembly.GetEntryAssembly().CodeBase;
objProcessInfo.UseShellExecute = true;
objProcessInfo.Verb = "runas";
try
{
Process proc = Process.Start(objProcessInfo);
Application.Current.Shutdown();
}
catch (Exception ex)
{
}
}
}
}