如何强制 WPF 应用程序在管理员模式下运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5276674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:39:20  来源:igfitidea点击:

How to force a WPF application to run in Administrator mode

wpfadminmode

提问by SVI

I have an WPF application which access windows services, task schedulers on the local machine. When I deploy this WPF application and run it without "Run as Administrator" , it fails as it is not able to access the windows services and task schedulers on the local machine. If I run it with "Run as Administrator", it works correctly.

我有一个 WPF 应用程序,它可以访问本地计算机上的 Windows 服务和任务调度程序。当我部署此 WPF 应用程序并在没有“以管理员身份运行”的情况下运行它时,它会失败,因为它无法访问本地计算机上的 Windows 服务和任务调度程序。如果我使用“以管理员身份运行”运行它,它可以正常工作。

How do I make my application by default run in admin mode when it is deployed in production?

在生产环境中部署时,如何让我的应用程序默认以管理员模式运行?

回答by vcsjones

You need to add an app.manifest. Change the requestedExecutionLevelfrom asInvokerto requireAdministrator. You can create a new manifest by using the add file dialog, change it to require administrator. Make sure that your project settings are set to use that manifest as well. This will allow you to simply double click the application and it will automatically prompt for elevation if it isn't already.

您需要添加一个app.manifest. 更改requestedExecutionLevelasInvokerrequireAdministrator。您可以使用添加文件对话框创建新清单,将其更改为需要管理员。确保您的项目设置也设置为使用该清单。这将允许您简单地双击应用程序,如果尚未提升,它将自动提示提升。

See here for more documentation:

有关更多文档,请参见此处:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

http://msdn.microsoft.com/en-us/library/bb756929.aspx

EDIT: For what it's worth, the article uses VS 2005 and using mt.exeto embed the manifest. if you are using Visual studio 2008+, this is built in. Simply open the properties of your Project, and on the "Application" tab you can select the manifest.

编辑:对于它的价值,本文使用 VS 2005 并mt.exe用于嵌入清单。如果您使用的是 Visual Studio 2008+,这是内置的。只需打开项目的属性,然后在“应用程序”选项卡上,您就可以选择清单。

回答by SLdragon

  1. Right-click your WPF project to add new Item: "Add->New Item..."
  2. Select "Application Manifest File" and click Add
  3. Double Click your newly created manifest file and change the
  1. 右键单击您的 WPF 项目以添加新项目:“Add->New Item...”
  2. 选择“应用程序清单文件”,然后单击“添加”
  3. 双击您新创建的清单文件并更改
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Then the WPF application would run as Administrator.

然后 WPF 应用程序将以管理员身份运行。

回答by M?rsh?ll

If you don't want broke the Clickonce this code is the best solution:

如果您不想破坏 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);
}

Founded in: http://matijabozicevic.com/blog/wpf-winforms-development/running-a-clickonce-application-as-administrator-also-for-windows-8

成立于:http: //matijabozicevic.com/blog/wpf-winforms-development/running-a-clickonce-application-as-administrator-also-for-windows-8

回答by Maghalakshmi Saravana

Steps to Make the WPF application to Run in Administrator Mode

使 WPF 应用程序以管理员模式运行的步骤

1.Open the Solution Explorer

1.打开解决方案资源管理器

2.Right CLick on the solution--->Add---->New Item---->App.Manifest---->OK

2.右键点击解决方案--->Add---->New Item---->App.Manifest---->OK

3.Edit the Manifest file as follows:

3.编辑Manifest文件如下:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

(TO)

(到)

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

4.After editing the Manifest file,Goto Solution project(RightCLick)------>properties------->Security

4.编辑Manifest文件后,转到解决方案项目(右键单击)------>属性------->安全

Turn out the Checkbox of "Enable ClickOnce Security Settings"

把“启用 ClickOnce 安全设置”的复选框去掉

  1. Run the Application, and Take setup, Now the application with Run as Administrator mode is acheived.
  1. 运行应用程序,并进行设置,现在以管理员身份运行的应用程序已实现。