Run application only in Admin account

RaviRanjanKr
Posted by RaviRanjanKr under C# category on | Points: 40 | Views : 2638
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();
}

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