5,665,355 members and growing! (15,609 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate License: The Code Project Open License (CPOL)

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

By Chris Richner

Shows how to build solution files from command line or via contextmenu item.
C#Windows, .NET, .NET 1.0, Win2K, WinXPVS.NET2002, Visual Studio, Dev

Posted: 8 Dec 2002
Updated: 13 Dec 2002
Views: 125,416
Bookmarked: 18 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 3.72 Rating: 3.16 out of 5
3 votes, 20.0%
1
1 vote, 6.7%
2
1 vote, 6.7%
3
3 votes, 20.0%
4
7 votes, 46.7%
5

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


Web


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 - now C#, WCF, WF, WinForms


Interests

  • basically coding

  • Keep my network and computers alive

  • Go out with my friends and listen to the tight house
    beats in da clubs.

Occupation: Software Developer (Senior)
Location: Switzerland Switzerland

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 33 (Total in Forum: 33) (Refresh)FirstPrevNext
QuestionDoes anybody know how to update web reference in a solution within command line?membergusenica3:30 25 Apr '08  
GeneralMy Versionsusseraserhead9:21 16 Oct '03  
GeneralDoes anyone know how to display the build log whilst command line builds are in progress?member[DAve]1:08 1 May '03  
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress?membernormanr5:37 16 Jul '04  
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress?member[DAve]5:55 16 Jul '04  
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress?memberM_A_MaDeRo6:44 25 Jul '05  
Generalproblem with devenv.exe (vs.net)membernagash118:01 7 Mar '03  
GeneralRe: problem with devenv.exe (vs.net)memberJerry Maguire3:49 8 Mar '03  
GeneralRe: problem with devenv.exe (vs.net)memberJerry Maguire6:06 12 Mar '03  
GeneralRunning from IISsussNikolay Simeonov10:03 16 Dec '02  
GeneralRe: Running from IISmemberJerry Maguire12:50 16 Dec '02  
GeneralRe: Running from IISsussNikolay Simeonov13:01 16 Dec '02  
GeneralRe: Running from IISmemberJerry Maguire13:16 16 Dec '02  
GeneralProject Path containing SPACEsmemberCoolDude3:13 16 Dec '02  
GeneralRe: Project Path containing SPACEsmemberJerry Maguire4:55 16 Dec '02  
GeneralREG FilememberRyan Farley11:07 10 Dec '02  
GeneralRe: REG FilememberJerry Maguire13:04 11 Dec '02  
GeneralGerman Screenshots,memberJerry Maguire13:14 9 Dec '02  
GeneralRe: German Screenshots,memberDaniel Turini22:12 9 Dec '02  
GeneralRe: German Screenshots,memberHeath Stewart2:52 10 Dec '02  
GeneralRe: German Screenshots,memberJerry Maguire3:22 10 Dec '02  
GeneralRe: German Screenshots,sussAnonymous4:04 10 Dec '02  
GeneralRe: German Screenshots,sitebuilderUwe Keim7:34 10 Dec '02  
GeneralRe: German Screenshots,sussAnonymous7:36 10 Dec '02  
GeneralRe: German Screenshots,sitebuilderUwe Keim8:00 10 Dec '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Dec 2002
Editor: Smitha Vijayan
Copyright 2002 by Chris Richner
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project