Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

How to invoke Visual Studio from the Windows Explorer Shell

4.47/5 (7 votes)
7 Dec 2009CPOL4 min read 39K  
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)