Friday, April 24, 2015

Solution Open with ... batch build

I often want to right-click a Visual Studio solution file (.sln) in Windows Explorer to ensure that it builds cleanly. I don't want to bother manually launching Visual Studio for the same purpose. Here's my way of doing this.

Create a batch file like the following (mine is called buildsln.bat). This command is rather simple, you can make one as fancy as you need.

@echo off
"C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild" "%1" /t:build /v:minimal
if errorlevel 1 pause

Import the following into the Registry (as an Administrator) to add an Open With menu selection when you right-click a .sln file.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Orthogonal.buildsln]
@="Build SLN"

[HKEY_CLASSES_ROOT\Orthogonal.buildsln\Shell\Open\Command]
@="\"C:\\ProgramData\\Utility\\buildsln.bat\" \"%1\""

[HKEY_CLASSES_ROOT\.sln\OpenWithProgids]
"Orthogonal.buildsln"=""

Change the key name Orthogonal.buildsln to be whatever suits you. Set the first default key value to be the text that is displayed in the context menu. The next default key value is the path to the batch file.

Note: Windows 7 to 10

After upgrading my development PC from Windows 7 to 10 and recreating the registry values above, I found that the Open With menu did not have my new item. Even worse, the OpenWithProgids key for .sln has 4 values, but only 2 of them appear in the Open With menu. Despite extensive experiments, I haven't figured out yet how to get my extra context commands to appear.

No comments:

Post a Comment