Testing that my application is being run as administrator

C

Claire

Hi,
I'm a complete beginner regarding authentication/permissions etc and have no
idea what/where to start implementing whatever I need in my C# applications.
I've been trying to find info on the web but I seem to be hopeless at
knowing the correct terminology to get any sort of positive search result.
I've just written a utility application that needs to run a series of third
party installation packages, writes to their inifiles after
installation,start/stop services, reads/writes files, registry etc etc.
Expected to be run on Win 2000, XP and Vista.
My solution was to do a check on application startup that it was being run
as administrator, no idea where to start in doing that, and no idea if
that's needed in Win2000 or XP anyway.
Can anyone help me please? Especially with pointers towards links to noob
aimed articles but some code samples for testing for administrator so i can
at least show the user a warning.
thanks :)
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,
I'm a complete beginner regarding authentication/permissions etc and have no
idea what/where to start implementing whatever I need in my C# applications.
I've been trying to find info on the web but I seem to be hopeless at
knowing the correct terminology to get any sort of positive search result.
I've just written a utility application that needs to run a series of third
party installation packages, writes to their inifiles after
installation,start/stop services, reads/writes files, registry etc etc.
Expected to be run on Win 2000, XP and Vista.
My solution was to do a check on application startup that it was being run
as administrator, no idea where to start in doing that, and no idea if
that's needed in Win2000 or XP anyway.
Can anyone help me please? Especially with pointers towards links to noob
aimed articles but some code samples for testing for administrator so i can
at least show the user a warning.
thanks :)

You should check if your current Principal is member of the admin
group:
Take a look at WindowsPrincipal.IsInRole method, you will get all you
need there
 
C

Claire

thanks ignacio :)
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

WindowsPrincipal pr = new WindowsPrincipal(windowsIdentity);

if (!pr.IsInRole(WindowsBuiltInRole.Administrator))

MessageBox.Show(gui.frmProcess_WrongPrivileges);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top