• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 827
  • Last Modified:

IsInRole says I'm a User, not an Administrator

I want to put code into my VB 2008 app to see whether the user is an Administrator or not. I've tried three different methods, and all are reporting that I'm not an Administrator (I'm just a User), even though Control Panel/Users clearly reports that I'm an Administrator. Further testing indicates that I get this result on Windows 7 (I've checked two different machines, one Win7 Home Premium and the other Win7 Pro), but with XP I get the proper Administrator response.

I've tried the following methods of checking (based on different suggestions I've seen on EE):

1) my.User.IsInRole(ApplicationServices.BuiltInRole.Administrator)

2)  Dim idMe As WindowsIdentity = WindowsIdentity.GetCurrent
     Dim prinMe As New WindowsPrincipal(idMe)
     If prinMe.IsInRole(WindowsBuiltInRole.Administrator) Then

3)     AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim WP As WindowsPrincipal
        WP = System.Threading.Thread.CurrentPrincipal
        If WP.IsInRole(WindowsBuiltInRole.Administrator) Then
Everything is reporting False. They all report true for WindowsBuiltInRole.User. Any ideas on why? Is there something different I should be doing on Win7?
  • 3
  • 3
1 Solution
Jacques Bourgeois (James Burger)PresidentCommented:
In Windows 7, an application always start under User privileges, even if the current user is an Administrator. The methods you used all reflect this.

In order to be able to detect if the use is administrator, the application should be started in administator mode, either by setting that condition in the shortcut properties, launching the application through the Run As Admistrator option of its contex menu, or by requiring administrator privileges under the UAC settings through the View Windows Settings of the Application tab of the project's Properties window.

Even there, my.user wont give you the right answer. The whole My namespace contains limited capacity versions of other namespaces in the framework.

Code 2) works when the application is started in Administrator mode. I haven't checked code 3).

ElrondCTAuthor Commented:
Thanks for the information. This is obviously more complicated than I thought. I'm going to need to think through and test how the different methodology works with some of the things my software is doing (all the while remembering that XP is different, since I have clients using both). Do you know where Vista fits in this (like 7 or like XP)?
Jacques Bourgeois (James Burger)PresidentCommented:
Vista is like Windows 7... or the reverse.

When Vista came along, they changed a lot of things security wise. That thing about Administrator vs User is one of the important ones.

Applications always start with User privileges even when run by an administrator.

But the other way around, an administrator can get into a session run by a regular user and start any application under his administrator credentials with the techniques I mentioned.

Security is increased because the administrator always have to give his password when running an application that needs administrator privileges. And this makes things a lot more interesting for the administrator, because he does not have to log off a regular user account in order to make administrative changes to a computer.

But for us, poor programmers, lots a new things to learn. You will find out about those differences as you go along, such as the fact that Documents and Settings is now called Users.

There are usually ways to easily overcome those differences, but that is something you have to learn to live with when you are dealing with multiple OS with the same application.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

ElrondCTAuthor Commented:
Yes, I'm aware of the redesign of the folders. Fortunately, VB makes some of that simpler because My.Computer.FileSystem.SpecialDirectories will automatically give the correct folder location, no matter which version of Windows.

Well, I can see I've got some unexpected additional work to do. Thanks for the guidance.
Jacques Bourgeois (James Burger)PresidentCommented:
Just another piece of guidance. Try to stay away of My, except for My.Resources and My.Settings.

My is just shortcuts to other classes in the framework. It has been conceived to help amateurs. They can find most of what they need to do with what they find in My. It might make things easier in the beginning, but it hides a lot of possibilities from you.

As an example, My.Computer.FileSystem has about 30 methods to work with files, directories and drives. System.IO.File as more than that only for files.

If you ever move to another language and are used to My, you will have problem switching because My is there only for VB. The framework classes and methods on the other hand are the same for all languages.

And most of the things you find there are as easy to make using the framework, you just have to look around a little more to find the namespace that contains the feature, such as the following that you will probably recognize:


Once you have found the namespace for special folders, you will also find a nice treasure under System.Environment, some you get in My, but a lot that are not there.
ElrondCTAuthor Commented:
OK. Thanks. I've always hated the "My" terminology anyway, though it's disgustingly ubiquitous in computer usage these days...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now