Windows如何决定是否显示UAC提示?
|
在我的VB6应用程序中,我打开其他EXE文件.我的应用程序运行没有任何UAC提示符,但我有一个EXE,它检查软件的更新.这将提示UAC提示.那么Windows如何决定是否显示UAC提示?我看到这个
link.那么这是否取决于我在我的应用程序中写的代码?有趣的是,我的应用程序(即主EXE文件)不提示UAC,而检查和下载更新的小EXE会提示UAC.我有所有的EXE文件经过数字签名.我已经看了以下链接:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa511445.aspx http://technet.microsoft.com/en-us/library/cc505883.aspx等一些. 但是我还不清楚.
您几乎肯定会碰到Windows
Installer Detection Technology兼容性启发式.
Windows将尝试检测应用程序何时是安装程序,可能需要升级.
所以,正如你所说:
我的猜测是这个CheckForUpdates.exe是触发兼容性启发式. 正确的做法是将“检查”可执行文件的汇编清单通知Windows,它不应该提升实用程序.这是通过清单中的asInvoker的requestedExecutionLevel完成的: AssemblyManifest.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="client"
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,wanna get the update now?" dialog. A button with a UAC shield on it
ExecuteAsAdmin(Application.ExecutablePath,"/downloadUpdate");
与助手功能: private Boolean IsUserAnAdmin()
{
//A user can be a member of the Administrator group,but not an administrator.
//Conversely,the user can be an administrator and not 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)
{
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);
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
