简体   繁体   中英

Check if a program is started via "Run as Administrator" or not

I wanted to detect if my program is started via "Run as Administrator" or not. On two machines it works by doing:

new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)

Because when I run programs, by default they don't run as Administrator, so I can check it like that.

However on yet another machine (this one has Win7 and UAC disabled) and when I start any program there it automatically starts with admin rights, without ever having to "Right click -> Run as Administrator". Can I somehow filter this instance out?

Background is simply, that on the very first start it should prevent the user from starting it elevated, but if the user starts it elevated every time anyways it shouldn't care.

Hope it makes sense, Matty

I found out that the "EnableLUA" setting in the Registry in "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" was actually set to 0 so UAC is completely disabled and starts everything as an Admin so I will just add a check for this value to be 1 additionally:)

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