AlexC
AlexC

Reputation: 433

How to prevent my program from running in a non-administrator account that has UAC turned off?

I'm using this line in the app.manifest in order to raise an UAC prompt that demands administrative privileges in order for my program to run:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

It works. However, I found out that if the program is executed in a standard user account and UAC is turned-off, the program still runs - without administrative privileges -.

What I want, is to prevent it from running in this scenario. Instead, it should give a message like: "Sorry, this program requires administrative privileges to run". And then it should close itself.

Most questions I found here in SO, are about how to elevate an application when UAC is turned off. That's not what I'm searching.

Thanks in advance for your help!

Upvotes: 3

Views: 297

Answers (1)

Aedvald Tseh
Aedvald Tseh

Reputation: 1917

You have to check if your user is administrator with this:

if (!WindowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
    // show messagebox "Sorry, this program requires administrative privileges to run"
    Application.Exit();
}

Upvotes: 3

Related Questions