我创建了一个示例 winforms 应用程序,一旦用户双击或执行,就需要提升 UAC 提示。
要提升相同的权限,已创建清单文件“WindowsFormsApp1.exe.manifest”并放置在“WindowsFormsApp1.exe”所在的位置,按照链接https://learn.microsoft.com/en-us/previous-versions/bb756929(v =msdn.10)?redirectedfrom=MSDN
清单文件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="Any CPU"
name="WindowsFormsApp1"
type="win32"/>
<description>Description of your application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="asInvoker"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
根据链接文档,清单必须嵌入到应用程序中。在可执行文件旁边放置一个单独的清单文件不会达到预期的效果。
将清单文件添加到您的项目中,然后使用项目属性页面在“图标和清单”部分下选择它。重建您的项目,应用程序应在您启动时提示提升权限。
在可执行文件旁边放置单独的清单文件何时停止工作?我过去常常为那些没有以管理员权限编写的前任执行此操作。这是一种选择批处理文件如何启动该 exe 的有用方法(即作为管理员或标准)。