Click here to Skip to main content
15,894,896 members
Articles / Programming Languages / C#

Add a UAC shield to a button when elevation is required for admin tasks

Rate me:
Please Sign up or sign in to vote.
4.55/5 (35 votes)
22 Apr 20072 min read 192.3K   2.5K   77  
Add a UAC shield to a button when elevation is required for admin tasks using the API, and elevate the process if required.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace hackman3vilGuy.CodeProject.VistaSecurity.ElevateWithButton
{
    public partial class AdminForm : Form
    {
        public AdminForm()
        {
            InitializeComponent();

            if (!VistaSecurity.IsAdmin())
            {
                this.Text += " (Standard)";
                VistaSecurity.AddShieldToButton(buttonGetElevation);
            }
            else
                this.Text += " (Elevated)";
        }

        private void buttonGetElevation_Click(object sender, EventArgs e)
        {
            if (VistaSecurity.IsAdmin())
            {
                DoAdminTask();
            }
            else
            {
                VistaSecurity.RestartElevated();
            }
        }

        private void DoAdminTask()
        {
            try
            {
                string fName = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
                    + @"\Microsoft\Windows\Start Menu\VISTA.TXT";
                if (System.IO.File.Exists(fName))
                {
                    System.IO.File.Delete(fName);
                    MessageBox.Show("VISTA.TXT deleted");
                }
                else
                {
                    System.IO.File.Create(fName).Close();
                    MessageBox.Show("VISTA.TXT created");
                }
            }
            catch (UnauthorizedAccessException uacEx)
            {
                MessageBox.Show(uacEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.ToString());
            }

        }

        private void buttonNoElevation_Click(object sender, EventArgs e)
        {
            DoAdminTask();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions