Click here to Skip to main content
15,885,309 members
Articles / Operating Systems / Windows

How to invoke Visual Studio from the Windows Explorer Shell

Rate me:
Please Sign up or sign in to vote.
4.47/5 (7 votes)
7 Dec 2009CPOL4 min read 38.4K   16   13
This article shows how to integrate Visual Studio into the Windows Explorer Shell and call Visual Studio commands without opening the IDE.

Introduction

This is my first article, so I'll start with very basic stuff.

The scenarios this article applies to are frequently encountered. Lots of times, we need to build a solution or a project in Visual Studio, but we're not really interested in looking at the code, so we don't need to open up the whole IDE which could take some precious time. This may happen if the code is from a trusted source, or is a known piece of code (we wrote it). Or simply, we just need to build it! Period.

I often use this simple trick to clean all the stuff on my machine and try to build my solution from scratch to see if it's all OK before committing any code into our TFS code repository.

Background

Just some basic knowledge of the Windows command line interpreter.

Analyzing the Visual Studio prompt

There's nothing "magic" happening behind the scenes when you build a solution in Visual Studio. In fact, it's the devenv.exe that does everything, and all it needs to know is just the path to the project or the solution file. This can also be done by opening the Visual Studio command prompt, that you can find in the Tools sub-folder from the Start menu. You will notice that this menu item is simply a link to the command line interpreter:

%comspec% /k ""C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86

where

  • %comspec% is a Windows environment variable that points to the command line interpreter (cmd.exe).
  • /k is a parameter to cmd.exe that tells it to carry out the command specified by the string and to stay opened.
  • The path to vcvarsall.bat is quoted twice, both to preserve white space and to limit the scope of the parameter passed to the command line interpreter.
  • x86, instead, is a parameter to the vcvarsall.bat script file.

If you've used this tool before, you know that it sets the right environment variables to use Visual Studio from the console, so from here, you can start Visual Studio by typing:

devenv

or:

devenv "C:\solution.sln"

to open a solution file in Visual Studio, or even:

devenv "C:\solution.sln" /[Build|Clean|Rebuild]

to, respectively, build the solution file, clean it, or rebuild it.

Using the code

It's now time to use what we've seen so far and to wrap this command into a menu item that will allow us to use Visual Studio directly from the context menu in Windows Explorer.

You surely know you can customize the menu items shown in the context menu for a specific file type. Let's start by creating a new one, clicking on the Tools > Folder Options menu item in the Windows Explorer toolbar. In the dialog that appears, go into the File Type tab, and look for the SLN (Microsoft Visual Studio) file extension. Click the Advanced and then the New buttons. Now we can insert a new menu item called Build solution with the associated command, that must be:

cmd.exe /k "call "C:\Program Files\Microsoft Visual 
   Studio 9.0\VC\vcvarsall.bat" & devenv "%1" /Build"

There are a couple of things worth noticing:

  • This command line is different from the one that the Visual Studio command prompt link uses to prepare the command line interpreter. It used the %comspec% system variable (remember?) instead of a direct call to the cmd.exe executable file. The reason is that you cannot use %comspec% in a menu item command line.
  • The vcvarsall.bat script is not executed directly in the new instance of the command line interpreter, but is executed through a call command. This allows composition of commands inside a single command line interpreter instance. The path to this script file may be different from the one in the example. Look in your Visual Studio installation folder.
  • The vcvarsall.bat script is called with no arguments. It automatically uses the x86 switch when none is provided.
  • & is used to concatenate commands.
  • %1 is the current file name with full path. Quoted to preserve white spaces.

Points of interest

You can now have fun and try any parameter combinations from those provided by the command line interpreter and devenv.

For example, the one I use is:

cmd.exe /c "echo === Building "%1" === & call 
  "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" & 
  devenv "%1" /Build & pause"

This command will:

  1. Print a header with the current action and file name (echo Building %1).
  2. Prepare the command line interpreter to use Visual Studio (call ... ).
  3. Build the solution (devenv %1 /Build).
  4. Wait for any key to be pressed (pause).
  5. Exit (thanks to the /c parameter passed to cmd.exe).

You can insert three new menu items that use the three main parameters available from devenv: /Build, /Clean, and /Rebuild so that you can perform the three actions from the context menu.

Moreover, if you want, you can remove the pause command and change the /C parameter to /K: this will leave the Visual Studio console open after the action has completed. Quite handy if you want to do other stuff later.

History

  • 12/7/2009: First published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Exprivia
Italy Italy
I am 29 and I have a degree in Computer Science at the University of Bari (Italy) that I achieved with Full Marks and Honors.
My main fields of interest are in the world of Windows and the .NET framework.
I am currently employed as a Senior ASP.net and Sharepoint developer at Exprivia.
In the past I used to develop software in VC++, WPF/Silverlight, C#, and a number of other .NET related tecnologies.

Comments and Discussions

 
GeneralUseful Pin
galbra18-Jan-10 2:57
galbra18-Jan-10 2:57 
Its very useful.
Thank you.
Generalmisleading title Pin
Ian Good9-Jan-10 11:31
Ian Good9-Jan-10 11:31 
GeneralRe: misleading title Pin
Massimiliano Giustizieri10-Jan-10 3:36
Massimiliano Giustizieri10-Jan-10 3:36 
QuestionCan we have a setup file for this Pin
PrashKum14-Dec-09 23:07
PrashKum14-Dec-09 23:07 
AnswerRe: Can we have a setup file for this Pin
Massimiliano Giustizieri16-Dec-09 1:37
Massimiliano Giustizieri16-Dec-09 1:37 
GeneralWindows 7 Pin
BobElward14-Dec-09 17:27
BobElward14-Dec-09 17:27 
GeneralRe: Windows 7 Pin
Massimiliano Giustizieri15-Dec-09 9:49
Massimiliano Giustizieri15-Dec-09 9:49 
GeneralRe: Windows 7 Pin
Ian Good9-Jan-10 11:26
Ian Good9-Jan-10 11:26 
QuestionZip?? Pin
staceyw12-Dec-09 18:16
staceyw12-Dec-09 18:16 
AnswerRe: Zip?? Pin
Massimiliano Giustizieri13-Dec-09 3:39
Massimiliano Giustizieri13-Dec-09 3:39 
QuestionZip?? Pin
staceyw12-Dec-09 18:16
staceyw12-Dec-09 18:16 
GeneralGood one! Pin
Dinesh Mani7-Dec-09 17:09
Dinesh Mani7-Dec-09 17:09 
GeneralRe: Good one! [modified] Pin
Massimiliano Giustizieri9-Dec-09 11:49
Massimiliano Giustizieri9-Dec-09 11:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.