Run Application Only in Administrator Account

Many time we have requirement to run application only as a administrator account, not application we need to secure single windows form at many time to give access to those  user who have not Admin account privilege.

Take a look how to accomplish this task using C# language in your program. To accomplish it you can use simple function name is isAdm() which is described below
to access class like WindowsIdentity you need to use System.Security.Principal namespace in your code file.
as

using System.Security.Principal;

and then create function name isAdm()
isAdm() function having boolean return type which will return true if apllication is running in Admin account else it will return false.
Code will be as

class AdmCheck
    {
        public static bool isAdm()
        {

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

How to use this Function in your program

you can simply use this function in your program at specifying isAdm function in your entry point. for this you need to open Program.Cs file where entry points resides.
After using isAdm function your code will be as given below

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (AdmCheck.isAdm())
            {
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("You must run this application as Administrator");
                Application.Exit();
            }

        }

Now its completed and you are ready to test your application that its running in Admin account or not.
when you will compile your code or executing Application .exe file you will notice it will run if its running as administrator account otherwise it will give you error which is mentioned while creating program in entry point.

for getting more details you should download code. have a click_Me to Download Source Code.

I’ve also posted this article in DotNetSpark.Com too.

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C#, Tips&Tricks and tagged , , . Bookmark the permalink.

3 Responses to Run Application Only in Administrator Account

  1. Jay Tiempon says:

    nice stuff ravi its easy to use and helped me to much.

  2. Pingback: How to find if a user is administrator or not | RranjanK's Blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s