Click here to Skip to main content
Click here to Skip to main content

Build solution from command line or with contexmenu item with Microsoft VS.NET

By , 13 Dec 2002
 

Sample Image - builditemincontextmenu.jpg

Introduction

This little article can give you a hint on how you can build your solution files quickly without running 6 Visual Studio applications that enlarge your scratch file till your computer is slow like my grandma.

First of all run %ProgramFiles%\Microsoft Visual Studio .NET\Common7\Tools\vsvars32.bat, if devenv.exe (Visual Studio IDE) could not be found from command line.

Command line switches of devenv.exe

To see this list, type devenv /? on command prompt

  • /build - build the specified solution configuration
  • /project - specifies the project to build instead of solution, must specify /build to use /project
  • /projectconfig - specifies project configuration to build, must specify /project to use /projectconfig
  • /out - write build output to specified file
  • /rebuild - like /build, but forces a clean first
  • /clean - clean up build outputs
  • /deploy - build the specified solution configuration and then deploy it
  • /run - run the specified solution configuration
  • /runexit - run the specified solution configuration and then terminate
  • /command - executes the specified internal command line after startup
  • /mditabs - use tabbed documents interface
  • /mdi - use MDI interface
  • /fn - use specified font name
  • /fs - use specified font size
  • /LCID - use specified language ID
  • /noVSIP - disables VSIP developers license key for VSIP testing
  • /safemode - only default environment and services load for stability
  • /resetskippkgs - allow VSPackages once flagged for loading failures to load again

Console Application BuilderHelper

This little console application is able to check the return value from devenv.exe and we can display the build log if there went something wrong.

The basic method looks like this:

System.Diagnostics.ProcessStartInfo is filled with needed information.

public int Build(string solutionFile, string solutionConfig)
{
    // get temp logfile path
    string logFileName = System.IO.Path.GetTempFileName();
    // populate process environment
    System.Diagnostics.ProcessStartInfo psi = 
        new System.Diagnostics.ProcessStartInfo();
    psi.FileName =@"devenv.exe";
    psi.ErrorDialog = true;
    psi.Arguments =  "\"" +solutionFile +"\"" +  
         @" /rebuild "+ solutionConfig 
         + " /out " + logFileName;

Now we start the process and wait for exit.

// start process
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
// instruct process to wait for exit
p.WaitForExit();
// get return code
int exitCode = p.ExitCode;
// free process resources
p.Close();

If devenv.exe did not return zero, we display the build log to the user.

// if there was a build error, display build log to console
if (exitCode != 0)
{
    System.IO.TextReader reader =  System.IO.File.OpenText(logFileName);
    errorLog = reader.ReadToEnd();
    reader.Close();
    //
    System.Console.WriteLine(errorLog);
    System.Console.WriteLine("Hit enter to abort...");
    System.Console.Read();
}
// delete temp logfile
System.IO.File.Delete(logFileName);

Last but not least, we return the code.

// return process exit code
    return exitCode;
}

Build Batch

  • Create a new text file in the directory where your solution file is located, be sure to change extension from .txt to .bat. If you can't see the extension in your Explorer, change Explorer settings in Tools/Options/View to show extensions.
  • Do not double click the file to edit, use the context menu item Edit or drag it to a text editor like Notepad.
  • Add line VsBuilderHelper.exe solutionfilename.sln solution configuration, where solution configuration is usually debug or release..
  • Save the file and close Notepad.
  • Double click now your *.bat file. If everything went right, the console closes after while, otherwise the build log is displayed.

Extend context menu with build item

  • Open Explorer, open Tools/Options.

File Extension Dialog

  • Change to tab "File Types".
  • Search for sln extension, type sln to find quickly.
  • Click on Extend button (every Windows version has another dialog for this).
  • Click New in the Property Dialog.
  • Choose a name for this item like Build Debug
  • In the path field, type: "VsBuilderHelper.exe" "%1" solution configuration, where solution configuration is usually debug or release. If your VsBuilderHelper.exe is not in the path environment, please specify the full path to VsBuilderHelper.exe.
  • Close the dialogs and go to any solution, right click solution file and choose your menu item. If everything went right, the console closes after a while, otherwise the build log is displayed.

Have phun with it...

Revision History

16.12.2002

  • Fixed bug with project path that contains spaces.
  • Build log stored in temporary file.
  • Console application returns process exit code to caller.

14.12.2002

  • Added console application BuilderHelper.
  • Removed direct devenv call samples.

License

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

About the Author

Chris Richner
Software Developer (Senior) Zeit AG
Switzerland Switzerland
Member
Biography
  • 1996 - 1998 PC Board PPL, HTML, DHTML, Javascript and ASP
  • 1999 - 2001 coding Centura against Sql Database (Centura,MSSQL,Oracle)
  • 2002 - 2004 C# Windows Forms
  • 2005 - 2006 C# ASP.NET, Windows Forms
  • 2006 - 2009 C#, WCF, WF, WPF
  • 2010 - 2012 C#, Dynamics CRM, Sharepoint, Silverlight
  • 2013 - now C#, WCF DS (OData), WF, WPF
Interests
  • family & friends
  • chilaxing ,)
  • coding

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 

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

You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDoes anybody know how to update web reference in a solution within command line? Pinmembergusenica25 Apr '08 - 2:30 
I have some solution those I want to aumatically rebuild but one of them contains web reference that need to update during rebuild process. Using recommendations in this article it is possible to automate rebuild of all solutions but not web references
GeneralMy Version Pinsusseraserhead16 Oct '03 - 8:21 
It always prints out the log, and it keeps a time stamp.
 
I tried to do a realtime log monitor but VS has a lock on the log file so it can't be read.
 
          public int Build(string solutionFile, string solutionConfig)
          {    
               // get temp logfile path
               string logFileName = System.IO.Path.GetTempFileName();
 
               Console.WriteLine("Building: " + solutionFile);
               Console.WriteLine("Built Started At: " + DateTime.Now);
               DateTime LStartTime = DateTime.Now;
 
               // populate process environment
               System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
               psi.FileName =@"devenv.exe";
               psi.ErrorDialog = true;
               psi.Arguments =   "\"" +solutionFile +"\"" +   @" /rebuild "+ solutionConfig + " /out " + logFileName;
               // start process
               System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
               // instruct process to wait for exit
               p.WaitForExit();
               // get return code
               int exitCode = p.ExitCode;
               // free process resources
               p.Close();
 
               // if there was a build error, display build log to console              
               System.IO.TextReader reader =   System.IO.File.OpenText(logFileName);
               errorLog = reader.ReadToEnd();
               reader.Close();                   
 
               System.Console.WriteLine(errorLog);
              
               Console.WriteLine("devenv.exe returned exit code: " + exitCode);
               Console.WriteLine("Build Ended At: " + DateTime.Now);
               Console.WriteLine("Build Took: " + (DateTime.Now - LStartTime).ToString());
 
               System.Console.WriteLine("Press Enter to Continue...");
               System.Console.Read();
 
               // delete temp logfile
               System.IO.File.Delete(logFileName);
               // return process exit code
               return exitCode;
          }

QuestionDoes anyone know how to display the build log whilst command line builds are in progress? Pinmember[DAve]1 May '03 - 0:08 
As the title says... Does anyone know how to display the build log whilst command line builds are in progress?
 
I find it very annoying to just have a cursor blinking at me (I can see it now) when I really want to know how far along the build is.
 
I have tried a tail util on the build log, but that does not work.
 
Anyone any ideas?
 
DAve
 

AnswerRe: Does anyone know how to display the build log whilst command line builds are in progress? Pinmembernormanr16 Jul '04 - 4:37 
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress? Pinmember[DAve]16 Jul '04 - 4:55 
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress? PinmemberM_A_MaDeRo25 Jul '05 - 5:44 
Generalproblem with devenv.exe (vs.net) Pinmembernagash17 Mar '03 - 17:01 
hi guys!
 
i got a problem. vs.net doesnt start.
instead i get an error message.
the pop-up window says: "ms development environment has determined a problem und got to be shut down" (sorry, translation from german version)
"an unhandled exception has been caught by the vsw exception filter."
appname:devenv.exe appver:7.0.9466.0 modname: hxds.dll
modver: 2.1.9466.0 offset: 0001e84f
im anything but skilled with this stuff, maybe one of you guys can help me out.
thank you!

GeneralRe: problem with devenv.exe (vs.net) PinmemberJerry Maguire8 Mar '03 - 2:49 
GeneralRe: problem with devenv.exe (vs.net) PinmemberJerry Maguire12 Mar '03 - 5:06 
GeneralRunning from IIS PinsussNikolay Simeonov16 Dec '02 - 9:03 
I have a problem trying to run it from the web server. It's weird - if someone is logged on to the console or using terminal client - it works. When noone is logged on - it doesn't.
 
I set all the neccessary permissions etc, but unless I log on to the server "devenv.exe" is freezing - not consuming the CPU, but doesn't do anything and the buildlog.txt file is locked until I kill the process.
 
Do you have any ideas why it happens like that and how can I fix it?
GeneralRe: Running from IIS PinmemberJerry Maguire16 Dec '02 - 11:50 
GeneralRe: Running from IIS PinsussNikolay Simeonov16 Dec '02 - 12:01 
GeneralRe: Running from IIS PinmemberJerry Maguire16 Dec '02 - 12:16 
Me too ,)
 
please try it with the updated code.
 
Have a nice day
 
.:Greets from Jerry Maguire:.
GeneralProject Path containing SPACEs PinmemberCoolDude16 Dec '02 - 2:13 
Hi,
i experience problems with the tool if a project path that contains spaces.Cry | :(( After changing on line in the code the tools works for me on those project paths.Smile | :)
 
Orig:
psi.Arguments = solutionFile + @" /rebuild "+ solutionConfig + " /out " + logFileName;
Modified:
psi.Arguments = "\"" +solutionFile +"\"" + @" /rebuild "+ solutionConfig + " /out " + logFileName;
 
Nice tool, thanks and regards, Christian
GeneralRe: Project Path containing SPACEs PinmemberJerry Maguire16 Dec '02 - 3:55 
GeneralREG File PinmemberRyan Farley10 Dec '02 - 10:07 
You could create a REG file to add this to the registry automatically. I've done this so anytime I install VS, I can just execute the REG file to create the context menu items.
 
Create a text file and add the following text. Then rename the text file to "something.reg". Then to add it to the registry just execute the reg file. This text adds both 'Build Debug' and 'Build Release' to the context menu (using XP reg version 5.00).
 

Windows Registry Editor Version 5.00
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug]
@="Build Debug"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\command]
@="\"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\IDE\\devenv.exe\" \"%1\" /rebuild Debug"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\ddeexec]
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\ddeexec\Application]
@="devenv"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\ddeexec\Topic]
@="System"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release]
@="Build Release"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\command]
@="\"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\IDE\\devenv.exe\" \"%1\" /rebuild Release"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\ddeexec]
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\ddeexec\Application]
@="devenv"
 
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\ddeexec\Topic]
@="System"

 
Anyway, thanks for the article. It is a good idea.
 
-Ryan
GeneralRe: REG File PinmemberJerry Maguire11 Dec '02 - 12:04 
GeneralGerman Screenshots, PinmemberJerry Maguire9 Dec '02 - 12:14 
Sorry for my bad english and the german screenshots, but i had no english XP/VS.NET by the hand.
 
.:Greets from Jerry Maguire:.
GeneralRe: German Screenshots, PinmemberDaniel Turini9 Dec '02 - 21:12 
GeneralRe: German Screenshots, PinmemberHeath Stewart10 Dec '02 - 1:52 
GeneralRe: German Screenshots, PinmemberJerry Maguire10 Dec '02 - 2:22 
GeneralRe: German Screenshots, PinsussAnonymous10 Dec '02 - 3:04 
GeneralRe: German Screenshots, PinsitebuilderUwe Keim10 Dec '02 - 6:34 
GeneralRe: German Screenshots, PinsussAnonymous10 Dec '02 - 6:36 
GeneralRe: German Screenshots, PinsitebuilderUwe Keim10 Dec '02 - 7:00 
GeneralRe: German Screenshots, PinmemberJerry Maguire10 Dec '02 - 8:59 
GeneralRe: German Screenshots, Pinsusssunhorse10 Dec '02 - 9:31 
GeneralRe: German Screenshots, PinmemberJerry Maguire10 Dec '02 - 10:31 
GeneralRe: German Screenshots, PinsitebuilderUwe Keim10 Dec '02 - 19:14 
GeneralRe: German Screenshots, PinmemberJerry Maguire11 Dec '02 - 12:10 
GeneralRe: German Screenshots, PinsitebuilderUwe Keim11 Dec '02 - 19:47 
GeneralRe: German Screenshots, PinsussNikolay Simeonov12 Dec '02 - 12:21 
GeneralRe: German Screenshots, PinmemberJerry Maguire14 Dec '02 - 8:33 
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 Dec 2002
Article Copyright 2002 by Chris Richner
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid