您几乎肯定正在触发 Windows Installer Detection Technology 兼容性启发式。Windows 将尝试检测应用程序是否是安装程序,并且可能需要提升权限。
安装程序检测仅适用于以下内容:
- 32位可执行文件
- 没有“requestedExecutionLevel”的应用程序
- 作为启用了LUA的标准用户运行的交互式进程
在创建32位进程之前,将检查以下属性以确定它是否为安装程序:
- 文件名包括关键字,如“install”,“setup”,“update”等。
- 版本资源字段中的关键字:供应商、公司名称、产品名称、文件说明、原始文件名、内部名称和导出名称。
- 嵌入在可执行文件中的并排清单中的关键字。
- 链接在可执行文件中的特定StringTable条目中的关键字。
- 链接在可执行文件中的RC数据中的关键属性。
- 可执行文件中的目标字节序列。
因此,正如您所说:
但我有一个检查软件更新的exe文件
我的猜测是,这个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="ITReasearchAssociates.Contoso.Updater"
type="win32"
/>
<description>Update checker</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
这样,您的“检查更新”应用程序将永远不会提升,并且永远不会错误地获得管理特权。
如果您希望您的更新程序实际应用需要管理特权的更新,则需要以管理员身份启动您的更新程序。
示例代码
if (!CheckForUpdatesAvailable())
return;
if (IsUserAnAdmin())
{
DownloadAndApplyUpdates();
return;
}
ExecuteAsAdmin(Application.ExecutablePath, "/downloadUpdate");
用辅助函数:
private Boolean IsUserAnAdmin()
{
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();
if (FindCmdLineSwitch("downloadUpdate", true))
{
DownloadAndApplyUpdates();
Environment.Exit(0);
}
}
请注意:任何代码均已释放到公共领域,无需归属。
mt.exe
按建议将清单文件添加到我的.exe
! 完美运行! - ewerybody