I’m building a small tool (more about it in a later post). What's important to successfully run this tool is that it should be executed with administrative privileges. In this post I share 2 ways how you can make this happen.
Remark: Be aware that both methods I show only work on Windows.
Check if the app is running using administrative privileges
My original solution was to check if the application was running with administrative privileges and exit if this was not the case.
Here is the code I used to check this:
This only checks if the current process is running as Administrator.
Request to run the app as administrator through UAC
The solution above works but wouldn’t it be more user friendly if we use UAC (User Account Control) and ask the user permission to elevate the permissions at startup?
This can be done by adding an App.Manifest file:
Inside this file add the following XML:
If we now run our program, we get a prompt to elevate the permissions:
Nice!