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:
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:
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!
Hi David, did you tried with %PROGRAMFILES% ?
ReplyDelete@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 :)
ReplyDeletecool and usfull :)
ReplyDeleteSteve
On Windows x64, %programfiles% redirects to C:\Program Files not C:\Program Files (x86).
ReplyDelete@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 :)
ReplyDeleteAnother 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.
ReplyDelete- SG
Run the following batch file.
ReplyDeleteset 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\""
I couldn't get compatibility mode to work for me. Whenever I double-click on the solution file, it just ignores it.
ReplyDeleteThanks for the tip.
ReplyDeleteJust run Visual Studio as administrator! I had issues with WCF projects long time back.
ReplyDelete- 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.
@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@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.
ReplyDeleteWhat can you not do in VS that requires admin?
ReplyDeleteThe 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)?
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@parrotlover77: The scenario I ran into was a solution that sets up an IIS app. It occurs in http://galleryserver.codeplex.com/
ReplyDeleteCould this URL be of any help?
ReplyDeletehttp://support.microsoft.com/kb/911089
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
ReplyDeleteWindows 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
there is a string
ReplyDelete"%PROGRAMFILES%\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe" "%1"
in those unicode bytes :)
"%PROGRAMFILES% will be expanded of course.
@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 :)
ReplyDeleteHi David,
ReplyDeleteI'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é