0

I have already opened Outlook i need to check it whether it is opened as administrator or not. I tried the code which is as follows but it checks if VS is opened as administrator or not.

    public bool IsRunningAsLocalAdmin()
    {
        WindowsIdentity cur = WindowsIdentity.GetCurrent();
        foreach (IdentityReference role in cur.Groups)
        {
            if (role.IsValidTargetType(typeof(SecurityIdentifier)))
            {
                SecurityIdentifier sid = (SecurityIdentifier)role.Translate(typeof(SecurityIdentifier));
                if (sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) || sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid))
                {
                    return true;
                }

            }
        }

        return false;
    }

I need to check whether Outlook is opened as Administrator or not.

Abdulla Jidda
  • 141
  • 1
  • 2
  • 8

1 Answers1

0

OpenProcess(PROCESS_QUERY_INFORMATION)+OpenProcessToken(TOKEN_QUERY) to get the token, then pass that token and the SID from CreateWellKnownSid(WinBuiltinAdministratorsSid) to CheckTokenMembership()

To be able to open (almost) every process for PROCESS_QUERY_INFORMATION access you need to be running as administrator and with debug privileges.

See Check if another process has admin privileges in .NET for more information.

Community
  • 1
  • 1
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45