是否可以为应用程序中的一个线程显示 UAC 对话框? [英] Is it possible to show UAC dialog for one thread in application?

查看:42
本文介绍了是否可以为应用程序中的一个线程显示 UAC 对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,想用具有管理员权限的新线程运行它,

I have a method and want to run it with new thread with admin privilege,

[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
    void Install()
    {
      //do some thing
    }

我是这样运行的,

installerTh = new Thread(new ThreadStart(Install));
        installerTh.Name = "Installer";
        installerTh.Start();

但它给了我错误

System.Security.SecurityException 未处理

System.Security.SecurityException was unhandled

Message=请求主体权限失败.

Message=Request for principal permission failed.

在运行此线程之前显示 UAC 窗口有什么想法吗?(或正在运行 Process )

Any Idea to show UAC window before running this thread ? (or in the middle of running Process )

推荐答案

UAC 要么全要么全无.您不能将 UAC 提示应用于单个线程,只能应用于进程.您需要做的是使用 UAC 使用特殊的命令行启动您的应用程序,命令行会通知您启动您想要的线程.它不必是命令行,您可以使用任何类型的 IPC 让生成的进程知道运行线程.我写了一个关于在 UAC 下启动进程的答案 这里.

UAC is all or nothing. You cannot apply a UAC prompt to a single thread, only to a process. What you need to do is launch your application with a special command line using UAC, and the command line will let you know to start the thread you want. It does not have to be a command line, you could use any type of IPC to let the spawned process know to run the thread. I wrote an answer about launching a process under UAC here.

您可能还需要考虑获取代码签名证书,以便 UAC 对话框不会一直说未知"作为发布者.

You might also want to consider getting a code signing certificate so that the UAC dialog won't keep saying "Unknown" as the publisher.

更新:根据您上面的评论,您也不能将 UAC 应用于当前正在运行的进程,只能应用于新进程.

Update: Based on your comment above, you also cannot apply UAC to your process currently running, only to a new process.

这篇关于是否可以为应用程序中的一个线程显示 UAC 对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆