.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Run Application Only in Administrator Account

Posted By:Ravi Ranjan Kumar       Posted Date: July 31, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

This article enable you to 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.
 

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
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.

using System.Security.Principal;
and then write given code

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 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.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend