56

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

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

4

5 回答 5

84

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

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

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

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

于 2011-03-11T18:19:16.387 回答
25
  1. 右键单击您的 WPF 项目以添加新项目:“添加->新项目...”
  2. 选择“应用程序清单文件”并单击添加
  3. 双击新创建的清单文件并更改
<requestedExecutionLevel level="asInvoker" uiAccess="false" />

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

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

于 2018-11-22T13:24:57.077 回答
4

使 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 安全设置”复选框

  1. 运行应用程序,并进行设置,现在以管理员身份运行的应用程序已实现。
于 2020-02-17T12:20:46.473 回答
3

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

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

于 2018-08-12T01:37:13.057 回答
1

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)
                {
                }
            }
        }
}
于 2021-11-11T09:55:51.183 回答