Link to home
Start Free TrialLog in
Avatar of LeighWardle
LeighWardleFlag for Australia

asked on

vb.net - How to check if current user is an administrator?

Hi Experts,

How can I check if the current user of my application is an administrator?

Regards,
Leigh
Avatar of John
John
Flag of Canada image

The NET USER command gives this to you. Run inside VB.
Avatar of LeighWardle

ASKER

Thanks, John.

I was hoping for a simple function call from VB.NET.
But I will use CMD if necessary.
Avatar of Wayne Taylor (webtubbs)
Use a native .Net method such as My.User.IsInRole.

See here -> https://msdn.microsoft.com/en-us/library/ms172996(v=vs.90).aspx
Thanks, Wayne.

I tried using the example on the Microsoft webpage you quoted.

Here's my code:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
            MsgBox("User is Administrator")
        Else
            MsgBox("User is not Administrator")
        End If

    End Sub
End Class

Open in new window


But I'm logged in as an Administrator, but the code is outputting:

User is not Administrator
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Wayne,

Thanks for the insights regarding Administrator mode.

The application works OK when I start it using Run As Administrator.

That meets my needs.

Regards,
Leigh