【问题标题】:How to force a WPF application to run in Administrator mode如何强制 WPF 应用程序以管理员模式运行
【发布时间】:2011-07-13 16:44:05
【问题描述】:

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

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

【问题讨论】:

    标签: wpf admin mode


    【解决方案1】:

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

    查看更多文档:

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

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

    【讨论】:

    • 这也适用于 Windows 7 吗?该页面上有一条提示提出了这个问题...在未来的版本中,运行提升的应用程序的唯一方法是拥有一个签名的应用程序清单,该清单标识了应用程序所需的权限级别。我>
    • @kzen,到目前为止 - 是的 - 同样的过程也适用于 Windows 7。
    • 如果我在 Windows Server 2008 机器上安装我的应用程序,这会起作用吗?那是我在运行我的应用程序时遇到的问题?
    • VCSJones,我尝试将 {MyAppliationName}.exe.Manifest 文件添加到项目中,但当我编译时,我得到一个似乎很常见的错误。错误是“ClickOnce 不支持请求执行级别‘requireAdministrator’。”
    • 感谢 VCSJones。你的解决方案对我有用。我必须禁用 ClickOnce 才能消除该错误。我通过转到项目属性、安全选项卡并取消选中“启用 ClickOnce 安全设置”选项来做到这一点。
    【解决方案2】:

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

    【讨论】:

    • 这是唯一不破坏 ClickOne 标志和安全性的解决方案。如果您想使用发布该解决方案。
    【解决方案3】:
    1. 右键单击您的 WPF 项目以添加新项目:“添加->新项目...”
    2. 选择“应用程序清单文件”并点击添加
    3. 双击新创建的清单文件并更改
    <requestedExecutionLevel level="asInvoker" uiAccess="false" />
    

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

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

    【讨论】:

    • 工作正常。 :)
    【解决方案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. 运行应用程序,并进行设置,现在以管理员身份运行模式的应用程序已实现。

    【讨论】:

    • 如果第 2 步没有可供选择的 App.Manifest 怎么办?我不知道我的项目发生了什么,但我真的没有清单文件可供选择使用 Visual Studio 2019 和 WPF c#
    【解决方案5】:

    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)
                    {
                    }
                }
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-08
      • 2013-06-17
      • 1970-01-01
      • 2011-02-18
      相关资源
      最近更新 更多