3

我有一个应用程序需要在具有提升权限的 Windows 8 上自动运行。我已经嵌入了包含 requestExecutionLevel 的 requireAdministrator 属性的清单,当通过鼠标单击运行时它可以正常工作,但是我需要应用程序自动运行。

这在 vista 和 windows 7 上运行良好,但在 windows 8 上运行良好......它根本无法运行。如何让它执行并提示用户许可?

干杯

4

2 回答 2

4

我没有任何可用于测试的 Windows 8 安装,但在 Click Once 部署(不允许使用清单)中需要管理员权限时遇到了类似的问题。

通过让应用程序在启动时检查它是否以管理员身份运行来解决它,如果它不是以管理员身份运行,它会以管理员身份重新启动。

这是我使用的代码(稍作修改):

public static bool IsRunningAsAdministrator()
{
    var wi = WindowsIdentity.GetCurrent();
    var wp = new WindowsPrincipal(wi);

    return wp.IsInRole(WindowsBuiltInRole.Administrator);
}

public static void StartAsAdmin(StartupEventArgs e)
{
    if (IsRunningAsAdministrator())
        return;

    // It is not possible to launch a ClickOnce app as administrator directly, so instead we launch the app as administrator in a new process.
    var processInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().CodeBase);

    // The following properties run the new process as administrator
    processInfo.UseShellExecute = true;
    processInfo.Verb = "runas";

    // Start the new process
    Process.Start(processInfo);

    // Shut down the current process
    Application.Current.Shutdown();
}

在多台机器上测试后更新:我在使用此解决方案时遇到的一个问题是防病毒软件经常将此视为可疑行为并阻止启动。

我最终将安装程序 + 自动更新从 Click Once 更改为WiX Toolset + NAppUpdaterequestedExecutionLevel="requireAdministrator"改为运行。

于 2012-10-03T08:04:20.773 回答
3

我需要应用程序自动运行。

我不确定您所说的“自动运行”是什么意思,但我假设您希望您的应用程序在用户登录、特定时间或满足其他触发条件时运行。您可以使用任务计划程序来实现这一点。如果用户具有管理权限,您可以请求任务计划程序以“最高”权限运行应用程序。为此,您必须在任务的属性中选中“以最高权限运行”:

任务属性

请注意,当任务计划程序执行该任务时,即使它以管理权限执行,也不会向用户显示 UAC 提示。

于 2012-10-03T08:16:00.467 回答