Windows如何决定是否显示UAC提示? [英] How does Windows decide whether to display the UAC prompt?
本文介绍了Windows如何决定是否显示UAC提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
http://msdn.microsoft.com/en-us/library/windows/desktop/aa511445.aspx
http://technet.microsoft.com/en-us/library/cc505883.aspx和其他一些。
但我还是不清楚。
推荐答案
您几乎肯定会遇到WindowsInstaller Detection Technology🕗兼容性试探法。 Windows将尝试检测应用程序何时是安装程序,可能需要提升。
安装程序检测仅适用于:
- 32位可执行文件
- 没有
的应用程序requestedExecutionLevel
- 以启用Lua的标准用户身份运行的交互进程
在创建32位进程之前,将检查以下属性以确定它是否为安装程序:
- 文件名包括"安装"、"安装"、"更新"等关键字。
- 以下版本控制资源字段中的关键字:供应商、公司名称、产品名称、文件描述、原始文件名、内部名称和导出名称。
- 可执行文件中嵌入的并排清单中的关键字。
- 可执行文件中链接的特定StringTable条目中的关键字。
- 可执行文件中链接的RC数据中的关键属性。
- 可执行文件中的目标字节序列。
那么,就像你说的:
但我有一个可用来检查软件更新的exe
我猜这CheckForUpdates.exe
正在触发兼容性试探法。
正确的做法是将程序集清单添加到"检查"可执行文件中,通知Windows它应该而不是提升该实用程序。这是通过清单中的requestedExecutionLevel
asInvoker
完成的:
Assembly Manifest.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="ITReasearchAssociates.Contoso.Updater"
type="win32"
/>
<description>Update checker</description>
<!-- Run as standard user. Disable file and registry virtualization -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
这样您的"检查更新"应用程序将永远不会提升,并且永远不会错误地获得管理权限。
如果希望更新程序实际应用更新(需要管理权限的更新),则应以管理员身份启动更新程序应用程序。
示例代码
//Check if there are updates available
if (!CheckForUpdatesAvailable())
return; //no updates. We're done
//If the user is an administrator, then get the update
if (IsUserAnAdmin())
{
//Maybe throw in a "Hey, user, wanna get the update now?" dialog
DownloadAndApplyUpdates();
return;
}
//The user is not an admin.
//Relaunch ourselves as administrator so we can download the update
//Maybe throw in a "Hey, user, wanna get the update now?" dialog. A button with a UAC shield on it
ExecuteAsAdmin(Application.ExecutablePath, "/downloadUpdate");
使用helper函数:
private Boolean IsUserAnAdmin()
{
//Public domain: no attribution required
//A user can be a member of the Administrator group, and yet not be an administrator.
//Conversely, the user can be an administrator while not being a member of the administrators group.
var identity = WindowsIdentity.GetCurrent();
return (null != identity && new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator));
}
private void ExecuteAsAdmin(string Filename, string Arguments)
{
//Public domain: no attribution required
ProcessStartInfo startInfo = new ProcessStartInfo(Filename, Arguments);
startInfo.Verb = "runas";
System.Diagnostics.Process.Start(startInfo);
}
然后在启动时,您只需查找/downloadUpdate
命令行参数即可知道您的工作是实际执行工作:
public Form1()
{
InitializeComponent();
//Ideally this would be in program.cs, before the call to Application.Run()
//But that would require me to refactor code out of the Form file, which is overkill for a demo
if (FindCmdLineSwitch("downloadUpdate", true))
{
DownloadAndApplyUpdates();
Environment.Exit(0);
}
}
注意:任何代码都会发布到公有领域。不需要属性。
这篇关于Windows如何决定是否显示UAC提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文