Visual Studio and Vista UAC with Open With Command

One of the issues of doing development with Visual Studio under Vista is that there are some cases where you need to be running Visual Studio as Administrator.  Normally you do this by right-clicking “Microsoft Visual Studio 2005” from the Start menu and selecting “Run as administrator”. UAC will then kick in and prompt you.  Once you dismiss the prompt you can then open your solution and continue to work with administrative privileges.

Unfortunately I generally don’t work like that as we have a single directory where our solution files are stored and I usually open Visual Studio by double-clicking the appropriate solution file.  If you right-click the solution file there is no obvious way of opening the file with administrative privileges (the equivalent of “Run as administrator” but for opening a file). 

To get around this I attempted to create a shortcut to DevEnv and set it to “Run as administrator” in its properties.  This works if you double-click the shortcut or if you use the Open With right-click menu item to open a solution file.  What it doesn’t do is remember this option in the list of Open With programs for that file type (unless you check “Always use the selected program to open this type of file” which is not what you want since you don’t always want to run devenv with admin privileges).

The solution to this is actually to use a console application, which Neil had conveniently already written, to effectively invoke devenv with administrative privileges as well as passing in any command line arguments (ie the solution you want to open).  Now, after using Open With and selecting this console application, I have an additional option in the Open With menu that allows me to “Start VS As Admin”.


Update: There is a slight bug in Neil’s code in that the arguments need to have parenthesis around them otherwise file names won’t be properly interpreted if the path has a space in it.