简体   繁体   中英

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. 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.

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).

Can anybody explain me how to solve my problem using simple and well documented .NET code, but not 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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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