Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Hub FS
Full List

Detecting if you are an Admin

This came up on an internal alias.  A customer wanted to know how to determine if there were running as an admin in a tool.  Below is a sample program that will print out whether or not you are the machine admin or a member of the machine administrators group. 

 

This is essentially the same as the code in a previous post of mine but ported to VB.  http://blogs.msdn.com/jaredpar/archive/2005/11/07/489942.aspx

Imports System.Security.Principal
Module Module1

    Function IsRunningAsLocalAdmin() As Boolean
        Dim cur As WindowsIdentity = WindowsIdentity.GetCurrent()
        For Each role As IdentityReference In cur.Groups
            If role.IsValidTargetType(GetType(SecurityIdentifier)) Then
                Dim sid As SecurityIdentifier = DirectCast(role.Translate(GetType(SecurityIdentifier)), SecurityIdentifier)
                If sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) OrElse sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) Then
                    Return True
                End If

            End If
        Next

        Return False
    End Function

    Sub Main()
        Console.WriteLine("Is Admin {0}", IsRunningAsLocalAdmin())
    End Sub

End Module

Published Wednesday, August 01, 2007 11:56 AM by Jared Parsons

Filed under: , ,

Comments

No Comments

New Comments to this post are disabled
Page view tracker