Open .sln File as Elevated User

by Jason Haley 4. January 2010 16:40

This is one of those things where I’m thinking there is a better way … I just haven’t found it yet –> if you know a better way please let me know

On all of my computers, I run as a non administrative user most of the time … so when I have to do things in Sql Server Management Studio (SSMS) or Visual Studio (VS) as administrator I just use the slickrun magic words I have set up for launching those apps as an elevated user.  However, when I want to open a .sln file from windows explorer as an elevated user I can’t.  Usually I just use slickrun to launch visual studio and then copy and paste the path into the open project dialog and away I go.

Today I decided that was dumb and I didn’t want to do that any more … I knew I could find a better way.  So I poked around in regedit for awhile looking at the other launchers I use, but most seem to work on the directory level and not a file level.  The ones that work on a file extension like Notepad2 (which shows for everything) don’t have an easy fix to elevate the application.  I tried some options using Elevate.cmd on the Open with submenu, but didn’t like the ugly icon that it used plus I needed two entries using the same method … so I wrote a simple little C# app to do it and used the nice looking shield icon from the VS2008ImageLibrary.  Of course I had to create two versions of the app (one for VS 2008 and one for 2010), but that was easy.

Now when I right click on an .sln file and select Open with, I get the context menu shown below:

image

The application is sort of a hack but it is simple and it works, here is the basic code:

class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
{
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = args[0];
start.FileName = <Path to VS>;
start.Verb = "runas";

var proc = new Process();
proc.StartInfo = start;
proc.Start();

Environment.Exit(0);
}
else
{
Usage();
}
}

static void Usage()
{
Console.WriteLine("Usage: ElevateVS20xx.exe \"slnPath\"");
}
}

Since I work with both VS 2008 and VS 2010, I wanted both versions as options.   To use it just do the following:

  1. Download the ElevatedVS.zip file
  2. Extract the zip to your local directory (NOTE: there are exe’s for VS 2008 and VS 2010 – x86 and x64 due to the Program Files directory being different).
  3. Copy the versions you need to a utility directory somewhere on your machine (I use c:\_utilities for things like this).

Now that you have the Exe’s locally, you need to connect them with the file extension (I’ve only focused on the .sln files but you can do whatever).

  1. In Windows Explorer, find a file with the extension you want to open with an Elevated Visual Studio and right-click on it
  2. On the context menu, choose Open with… | Choose default program …

The Open with dialog will show.

image    

    3.  Uncheck the Always use the selected program to open this kind of file check box
    4.  Click on the Browse… button
    5.  Browse to the location you put the ElevateVS2008.exe and ElevateVS2010.exe applications

The Open with dialog will then appear with the application you just selected at the bottom, like shown below

image

    6.  Click OK

This will cause the ElevateVS20xx to be called, so you’ll be prompted to enter the credentials – you can click No on the login if you don’t want to verify the thing works.

If you want to add multiple (ie. VS 2008 and VS 2010), you’ll need to repeat the steps above for the additional one.

Now when you right click on the .sln file and choose the Open With submenu, the new Elevated Visual Studio 20xx should be on there.

Comments (7) | Post RSSRSS comment feed |

Categories: Utility
Tags: