控制台应用程序需要UAC
我有一个控制台应用程序,需要使用一些需要管理员级别的代码。 我读到我需要添加一个清单文件 myprogram.exe.manifest ,如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator">
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
但它仍然不会引发 UAC(在控制台中或在 VS 中调试)。 我该如何解决这个问题?
更新
如果我在管理员中运行解决方案或在管理员中运行 /bin/*.exe ,我就可以使其工作。 我仍然想知道是否有可能在应用程序启动时弹出一些内容,而不是明确右键单击>以管理员身份运行?
I have a console application that require to use some code that need administrator level. I have read that I need to add a Manifest file myprogram.exe.manifest that look like that :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator">
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
But it still doesn't raise the UAC (in the console or in debugging in VS). How can I solve this issue?
Update
I am able to make it work if I run the solution in Administrator or when I run the /bin/*.exe in Administrator. I am still wondering if it's possible to have something that will pop when the application start instead of explicitly right click>Run as Administrator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于任何使用 Visual Studio 的人来说,这都非常简单。 我正要设置 Windows SDK 并执行 mt.exe 构建后步骤以及所有这些,然后才意识到它已内置到 VS 中。 我想我应该把它记录下来供后代使用。
Ta-da
For anyone using Visual Studio, it's super easy. I was about to go set up the Windows SDK and do mt.exe post-build steps and all that before realizing it's built into VS. I figured I'd record it for posterity.
Ta-da
斯科特的回答将按照您的要求进行操作,但 Microsoft 建议控制台应用程序显示“访问被拒绝”消息,而不是提示提升权限。
来自http://msdn.microsoft.com/en-us/library/bb756922。 ASPX:
我的 C# 代码如下。 它在 Windows XP(管理员 -> 确定,标准用户 -> 拒绝)和 Windows Server 2008(提升的管理员 -> 确定,非提升的管理员 -> 拒绝,标准用户 -> 拒绝)上进行了测试。
Scott's answer will do what you asked, but Microsoft recommends that console applications display an "access denied" message rather than prompt for elevation.
From http://msdn.microsoft.com/en-us/library/bb756922.aspx:
My C# code for this is below. It is tested on Windows XP (administrator -> ok, standard user -> denied) and Windows Server 2008 (elevated administrator -> ok, non-elevated administrator -> denied, standard user -> denied).
您需要将 UAC 清单嵌入为嵌入式 Win32 资源。 请参阅将 UAC 清单添加到托管代码。
简而言之,您使用 Windows SDK 命令行工具将其嵌入到可执行文件中。
您可以通过将以下行作为构建后任务放置在 VS 项目的属性中,将其作为构建后步骤自动执行:
You need to embed the UAC manifest as an embedded Win32 resource. See Adding a UAC Manifest to Managed Code.
In short, you use a Windows SDK command line tool to embed it into your executable.
You can automate this as a post-build step by placing the following line as a post build task in your VS project's properties: