Monday, April 4, 2011

Open your solution files as admin

When you run under UAC (User Account Control), nothing runs as admin by default, and that’s a good thing! But sometimes, you do need to run some things as administrator.

There are a few well known ways of doing this. You can right click on an EXE and choose ‘Run As Admin’. Or if you have the app pinned on your taskbar, you can Ctrl-Shift click it to run as admin. If you don’t know those tricks, you should learn them as they often come handy.

However, there is one common scenario for which there is no well documented technique: how do you launch a program as admin from a data file? Taking a particularly interesting example, how do you launch Visual Studio as admin from a .sln file?

First, you try the obvious and right click it, hoping to see the familiar ‘Run As Administrator’ item. But no luck there:

image

While this at first appears hopeless, it turns that there is a way to do this by adding some simple things to your registry.

The general technique is explained here (thanks to @meligy for pointing me to it). The post describes how to do it for any file type, but I can save you a bit of time by giving you the reg change you need to make (and it’s not scary!):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln\Shell\runas\Command]
@="\"C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\MSEnv\\VSLauncher.exe\" \"%1\""

Just save that in a foo.reg file somewhere and run it. After you do that, right clicking on a .sln file will look like this:

image

And that’s it, just what we wanted!

Final note: my reg file above is hard coded to “C:\\Program Files (x86)”, which won’t work on all systems so you may need to adjust things. I tried to change it to use the ProgramFiles(x86) env variable but I couldn’t make that work in the registry. Seems default values can’t be REG_EXPAND_SZ? Let me know if you know how to do this!

20 comments:

  1. Hi David, did you tried with %PROGRAMFILES% ?

    ReplyDelete
  2. @fabian: the problem is that environment variables don't get expanded (though you're right that %programfiles% would be the right one to use). Give it a try :)

    ReplyDelete
  3. On Windows x64, %programfiles% redirects to C:\Program Files not C:\Program Files (x86).

    ReplyDelete
  4. @Mohamed: no, I think %ProgramFiles% goes to "C:\Program Files (x86)" (at least it does for me!). But anyway, the primary issue here is how to get the env variable to expand :)

    ReplyDelete
  5. Another good way is setting the properties of Visual Studio Version Selector or Visual Studio itself to always run as administrator using compatibility settings. This will always run it as admin, however.

    - SG

    ReplyDelete
  6. Run the following batch file.

    set ProgramFilesx86=%ProgramFiles%

    if not ("%PROGRAMFILES(x86)%") == ("") set ProgramFilesx86=%ProgramFiles(x86)%


    REG ADD "HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln\Shell\runas\Command" /v ClientName /t REG_SZ /d "\\Common Files\\Microsoft Shared\\MSEnv\\VSLauncher.exe\" \"%1\""

    ReplyDelete
  7. I couldn't get compatibility mode to work for me. Whenever I double-click on the solution file, it just ignores it.

    ReplyDelete
  8. Just run Visual Studio as administrator! I had issues with WCF projects long time back.

    - Goto %programfiles%\Microsoft Visual Studio 9.0\Common7\IDE folder
    - Right click on devenv.exe
    - Goto compatibility tab
    - Select run as administrator option and select save

    Now you VS will always run as administrator.

    ReplyDelete
  9. @Guilherme: interesting approach using a batch file to expand the env variable! Note that you don't have to do the conditional logic. Using %ProgramFiles% works on all systems to refer to the 32 bit folder.

    ReplyDelete
  10. @Sjoerd and @Mohit: Yes, you can set VS to always run as admin, but the main point of this technique is to allow you to choose whether you want to run as admin on a solution by solution basis. In most cases, I don't need to run as admin, but when I do, I want a simple way.

    ReplyDelete
  11. What can you not do in VS that requires admin?

    The only thing I can think of off-hand is if you need to debug a process running as another user. For example, if you are debugging a service. Or if you are debugging through IIS (why would you do that when the built-in web host works so well)?

    ReplyDelete
  12. Meh, turn off UAC and run in an otherwise protected environment. This is not optimal for everyone. Like people who open attachments in emails claiming "I am a very nice Russian girl ..."

    ReplyDelete
  13. @parrotlover77: The scenario I ran into was a solution that sets up an IIS app. It occurs in http://galleryserver.codeplex.com/

    ReplyDelete
  14. Could this URL be of any help?
    http://support.microsoft.com/kb/911089

    ReplyDelete
  15. It looks a little bit tricky, but here is your file, that uses REG_EXPAND_SZ data type ('hex(2)'), and launcher path encoded into unicode bytes

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln\Shell\runas\Command]
    @=hex(2):22,00,25,00,50,00,52,00,4F,00,47,00,52,00,41,00,4D,00,46,00,49,00,4C,00,45,00,53,00,25,00,5C,00,43,00,6F,00,6D,00,6D,00,6F,00,6E,00,20,00,46,00,69,00,6C,00,65,00,73,00,5C,00,4D,00,69,00,63,00,72,00,6F,00,73,00,6F,00,66,00,74,00,20,00,53,00,68,00,61,00,72,00,65,00,64,00,5C,00,4D,00,53,00,45,00,6E,00,76,00,5C,00,56,00,53,00,4C,00,61,00,75,00,6E,00,63,00,68,00,65,00,72,00,2E,00,65,00,78,00,65,00,22,00,20,00,22,00,25,00,31,00,22,00

    ReplyDelete
  16. there is a string

    "%PROGRAMFILES%\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe" "%1"

    in those unicode bytes :)
    "%PROGRAMFILES% will be expanded of course.

    ReplyDelete
  17. @InTRUEdeR: tricky stuff! On problem is that if you give people that reg file, they won't trust it as easily because it's hard to see what it does :)

    ReplyDelete
  18. Hi David,

    I'm using this solution for a longer time:


    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln\Shell\Open Solution elevated]
    "HasLUAShield"=""

    [HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln\Shell\Open Solution elevated\Command]
    @="\"C:\\Windows\\nircmd.exe\" elevate \"C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\MSEnv\\VSLauncher.exe\" \"%1\""


    get nircmd.exe from here:

    http://www.nirsoft.net/utils/nircmd.html

    The advantage is that you can have more entries (there is only 1 RunAs) and I add the UAC shield (HasLUAShield).

    André

    ReplyDelete