Friday, September 14, 2007 4:34 PM bart

Simple managed code UAC elevation tool

Everyone knows UAC by now. One core element of Vista's UAC vision is the elevation of processes, used to run something as an administrator. Typically, you open up a command line instance (cmd.exe) which has been elevated already prior to invoking commands that require elevation. But what if you're already in a non-elevated command prompt window and need to start an executable that runs elevated? In a such a scenario a tool like the one shown below might be useful:

using System;
using System.Diagnostics;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Usage: {0}.exe <command>", Assembly.GetExecutingAssembly().GetName().Name);
            return;
        }

        string @params = "";
        if (args.Length > 1)
            @params = string.Join(" ", args, 1, args.Length - 1);

        ProcessStartInfo psi = new ProcessStartInfo(args[0], @params);
        psi.Verb = "runas";
        Process.Start(psi);
    }
}

This tool uses the regular Process class from the .NET Framework but specifies a Verb on the ProcessStartInfo object. You can find more info on verbs in the Windows SDK under "Verbs and File Associations". Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

No Comments