Question

I've wrote the Visual Studio add in which allows you to attach to some processes, but to be able to attach to process you have to run Visual Studio as Administrator. How can I check programmatically if Visual Studio is runned as Administrator or not, to show appropriate message for user?

Was it helpful?

Solution

The add-in you wrote will be written in either Visual Basic or Visual C#.

Thus like any other .NET application your add-in code can query the

System.Security.Principal.WindowsIdentity

and get the current user identity and principal and identify if the user is authenticated and has windows principal in build-in administrator role or not.

Sample code to verify if this Visual studio instance is done using "Run As Administrator" or not.

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);

Above code is in C#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top