How to check if application is running as administrator VB.NET
Asked Answered
P

4

7

I have made an application and some of its features only work with admin rights,

How can I check if application is running with admin rights or not ?

  • And show a message box if application is not running with admin rights to run as administrator.
Promptitude answered 19/4, 2017 at 11:42 Comment(0)
B
16
Imports System.Security.Principal

Dim identity = WindowsIdentity.GetCurrent()
Dim principal = new WindowsPrincipal(identity)
Dim isElevated as Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator) 
If isElevated Then
  MessageBox.Show("Is Admin")
End If

In VB.Net there is even a shortcut for this:

If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrat‌​or) Then ... 
Basswood answered 19/4, 2017 at 11:48 Comment(3)
This should be the accepted answer. It answers the OP's question, and I tested the solution and it works perfectly.Pagoda
Something is wrong in Windows 10 with this code, all the users are reported like administrators, even Guests without Administrator roleDonalddonaldson
Works fine on my Windows 10.Weighty
P
3

Just change the app.manifest to force require administration:

Solution Explorer --> My Project --> View Windows Settings

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Pancake answered 19/4, 2017 at 11:47 Comment(1)
Downvoted. Doesn't answer/address the specific question.Mcgruder
M
0

You can try and create a file on the system drive inside a try/catch block, and if it catches an access denied exception, then that means that the app is not running as administrator.

Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            File.Create("c://test.txt")
            MessageBox.Show("Is Admin")
            File.Delete("c://test.txt")
        Catch ex As Exception
            If ex.ToString.Contains("denied") Then
                MessageBox.Show("Is Not Admin")
            End If
        End Try
    End Sub
End Class
Methodius answered 20/4, 2017 at 9:23 Comment(4)
Too much lame answer. I got it in my mind, too. But it's definitely not for production environment.Bilection
Tricky but not proper answer, Use HKLM instead of C drivePlenish
Great answer. I love creativity like this.Mcgruder
@Mcgruder Thank you, yes me too and now that I'm looking at it after 6 years, it's kinda flabbergasted. Thank you!Methodius
C
0

A version for C# 6 (or later), adapted from the Visual Basic solution posted elsewhere on this page. In the interest of general-purpose use1, here I conceive a bool-valued static property:

(using System.Security.Principal;)

public static bool IsAdministrator =>
    new WindowsPrincipal(WindowsIdentity.GetCurrent())
            .IsInRole(WindowsBuiltInRole.Administrator);


Now having shown this, one must acknowledge that @Mederic's approach does indeed seem superior, since it's unclear precisely what an app might helpfully do after presumably detecting—and reporting—that such a (presumably) critical precondition has failed. Surely it's wiser—and safer—to delegate concerns of this nature to the OS.



1 That is, eliding the "MessageBox" desiderata articulated by the OP.

Cancel answered 20/12, 2017 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.