简体   繁体   English

以编程方式检查用户是否具有足够的权限来安装软件

[英]Programmatically check if user has enough rights for software installation

In my application (it supports only Windows 7 and higher), I want to run a script, which will run some installation program. 在我的应用程序中(它仅支持Windows 7和更高版本),我想运行一个脚本,它将运行一些安装程序。 Before doing it, I need to check if running this script from my app will provide it with enough rights that it could run installation. 在执行此操作之前,我需要检查从我的应用程序运行此脚本是否将为其提供足够的权限以使其可以运行安装。 Otherwise, Windows refuses to do it and shows a message with explanation that Admin rights are required for the user who launches my app. 否则,Windows拒绝这样做,并显示一条消息,说明启动我的应用程序的用户需要管理员权限。

According to this question , the easy way of solving my problem does not always work properly, but I also don't like the other suggestions (using Win API). 根据这个问题 ,解决我的问题的简单方法并不总是能正常工作,但是我也不喜欢其他建议(使用Win API)。

Can anybody explain me how to solve my problem using simple and well documented .NET code, but not Win API? 有人可以向我解释如何使用简单且有据可查的.NET代码解决我的问题,而不是Win API吗?

I know this doesn't match with your "but not Win API?", but really, using P/Invoke and the Win32 API is the way to go. 我知道这与您的“不是Win API吗?”不匹配,但实际上,使用P / Invoke和Win32 API是可行的方法。

As process elevation is related to Windows only (so it doesn't make sense if you want to use your code with Mono for instance), relying on the Win32 API is OK. 由于进程提升仅与Windows相关(因此,例如,要将代码与Mono一起使用就没有意义),因此依赖Win32 API是可以的。

GetTokenInformation is the good function to use, just make sure you use IntPtr for platform-specific sizes (or even better: SafeHandle for process handle) so it'll work fine on both x86/x64 machines. GetTokenInformation是一个很好的函数,只需确保将IntPtr用于特定于平台的大小即可(甚至更好:对于进程句柄使用SafeHandle ),使其在x86 / x64机器上都能正常工作。

You could try to ask for administrator rights first: 您可以尝试先要求管理员权限:
here , this will ask the user for administrator privileges when running the program. 在这里 ,这将在运行程序时询问用户管理员权限。 note: you might want to relaunch your IDE as administrator in case you will be asked to launch the debug as administrator every single time 注意:您可能想以管理员身份重新启动IDE,以防每次被要求以管理员身份启动调试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM