Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Codes Author
Wed, 23-Jul-2014 Authors
Zondray
40
All Time Authors
Satyapriyanayak
12160
Jayakumars
8120
Niladri.biswas
6000

Latest members | More ...


(Statistics delayed by 5 minutes)

Run application only in Admin account

RaviRanjanKr
Posted by RaviRanjanKr under C# on 7/31/2011 2:14:50 PM | Points: 40 | Views : 2009 | Status : [Member]

You need to use Security.Principal namespace to access WindowsIdentity namespace as

using System.Security.Principal;

and then you can use given function to check application is running in admin account or not.

public static bool isAdm()

{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
return false;
}
else
{
return true;
}
}


In Entry point of your application try to write given code
if (isAdm())

{
// Execute block when isAdmin return true
Application.Run(new Form1());
}
else
{
// Execute block when isAdmin return false
MessageBox.Show("You must run this application as Administrator");
Application.Exit();
}


Thanks
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com
Found interesting? Add this to:



Comments or Responses

Posted by: T.saravanan on: 8/2/2011 | Level:Silver | Status: [Member] [MVP] | Points: 10

Hi RaviRanjanKumar,

Nice info...

Posted by: RaviRanjanKr on: 8/2/2011 | Level:Starter | Status: [Member] | Points: 10

I am glad You liked it.

Posted by: Jayeshl on: 8/10/2011 | Level:Starter | Status: [Member] | Points: 10

good job man that helps me to do such kind of roles task for my application

Thanks
jayesh l
http://sqlassistant.blogspot.com

Posted by: RaviRanjanKr on: 8/10/2011 | Level:Starter | Status: [Member] | Points: 10

I am so glad that it helped you. :)


Login to post response