Click here to Skip to main content
Click here to Skip to main content
Go to top

How to invoke Visual Studio from the Windows Explorer Shell

, 7 Dec 2009
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Massimiliano Giustizieri
Software Developer Talentia Software
Italy Italy
I am 28 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 software engineer at Talentia Software (formerly known as Cezanne Software), where I started working at the age of 22.
In the past I used to develop software in VC++ and ASP.net. Currently I develop and mantain a Saas application with WPF/Silverlight, using XAML and C#, and a number of other .NET related tecnologies.

You may also be interested in...

Comments and Discussions

 
GeneralUseful Pinmembergalbra18-Jan-10 2:57 
Generalmisleading title PinmemberIGood9-Jan-10 11:31 
GeneralRe: misleading title Pinmembermaxilboia10-Jan-10 3:36 
QuestionCan we have a setup file for this PinmemberMember 476446714-Dec-09 23:07 
AnswerRe: Can we have a setup file for this Pinmembermaxilboia16-Dec-09 1:37 
GeneralWindows 7 Pinmemberelwardrm14-Dec-09 17:27 
GeneralRe: Windows 7 Pinmembermaxilboia15-Dec-09 9:49 
GeneralRe: Windows 7 PinmemberIGood9-Jan-10 11:26 
QuestionZip?? Pinmemberstaceyw12-Dec-09 18:16 
AnswerRe: Zip?? Pinmembermaxilboia13-Dec-09 3:39 
QuestionZip?? Pinmemberstaceyw12-Dec-09 18:16 
GeneralGood one! PinmemberDinesh Mani7-Dec-09 17:09 
GeneralRe: Good one! [modified] Pinmembermaxilboia9-Dec-09 11:49 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 7 Dec 2009
Article Copyright 2009 by Massimiliano Giustizieri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid