Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
4.35/5 (17 votes)
13 Dec 2002CPOL3 min read 229.6K   1.1K   31   33
Shows how to build solution files from command line or via contextmenu item.

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.

C#
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.

C#
// 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.

C#
// 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.

C#
// 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)


Written By
Software Developer (Senior)
Switzerland Switzerland
My interest is in the future because I am going to spend the rest of my life there. (Charles Kettering)

Biography

  • 1996 - 1998 PC Board PPL, HTML, DHTML, Javascript and ASP
  • 1999 - 2001 coding Centura against Sql Database (SqlBase,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 - 2013 C#, WCF DS (OData), WF, WPF
  • 2014 - 2016 C#, Azure PaaS, Identity, OWIN, OData, Web Api
  • 2017 - now C#, aspnet.core, IdentityServer4, TypeScript & Angular @ Azure IaaS or PaaS

Interests

  • family & friends
  • chilaxing ,)
  • coding

Comments and Discussions

 
QuestionDoes anybody know how to update web reference in a solution within command line? Pin
gusenica25-Apr-08 2:30
gusenica25-Apr-08 2:30 
GeneralMy Version Pin
eraserhead16-Oct-03 8:21
eraserhead16-Oct-03 8:21 
QuestionDoes anyone know how to display the build log whilst command line builds are in progress? Pin
[DAve]1-May-03 0:08
[DAve]1-May-03 0:08 
AnswerRe: Does anyone know how to display the build log whilst command line builds are in progress? Pin
normanr16-Jul-04 4:37
normanr16-Jul-04 4:37 
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress? Pin
[DAve]16-Jul-04 4:55
[DAve]16-Jul-04 4:55 
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress? Pin
M_A_MaDeRo25-Jul-05 5:44
M_A_MaDeRo25-Jul-05 5:44 
Generalproblem with devenv.exe (vs.net) Pin
nagash17-Mar-03 17:01
nagash17-Mar-03 17:01 
GeneralRe: problem with devenv.exe (vs.net) Pin
Chris Richner8-Mar-03 2:49
Chris Richner8-Mar-03 2:49 
GeneralRe: problem with devenv.exe (vs.net) Pin
Chris Richner12-Mar-03 5:06
Chris Richner12-Mar-03 5:06 
GeneralRunning from IIS Pin
nsimeonov16-Dec-02 9:03
nsimeonov16-Dec-02 9:03 
GeneralRe: Running from IIS Pin
Chris Richner16-Dec-02 11:50
Chris Richner16-Dec-02 11:50 
GeneralRe: Running from IIS Pin
nsimeonov16-Dec-02 12:01
nsimeonov16-Dec-02 12:01 
GeneralRe: Running from IIS Pin
Chris Richner16-Dec-02 12:16
Chris Richner16-Dec-02 12:16 
GeneralProject Path containing SPACEs Pin
CoolDude16-Dec-02 2:13
CoolDude16-Dec-02 2:13 
GeneralRe: Project Path containing SPACEs Pin
Chris Richner16-Dec-02 3:55
Chris Richner16-Dec-02 3:55 
GeneralREG File Pin
Ryan Farley10-Dec-02 10:07
Ryan Farley10-Dec-02 10:07 
GeneralRe: REG File Pin
Chris Richner11-Dec-02 12:04
Chris Richner11-Dec-02 12:04 
GeneralGerman Screenshots, Pin
Chris Richner9-Dec-02 12:14
Chris Richner9-Dec-02 12:14 
GeneralRe: German Screenshots, Pin
Daniel Turini9-Dec-02 21:12
Daniel Turini9-Dec-02 21:12 
Oh, and your keyboard was not working all right, so you needed to change some 'S' for '$', right? Poke tongue | ;-P

I see dumb people
GeneralRe: German Screenshots, Pin
Heath Stewart10-Dec-02 1:52
protectorHeath Stewart10-Dec-02 1:52 
GeneralRe: German Screenshots, Pin
Chris Richner10-Dec-02 2:22
Chris Richner10-Dec-02 2:22 
GeneralRe: German Screenshots, Pin
Anonymous10-Dec-02 3:04
Anonymous10-Dec-02 3:04 
GeneralRe: German Screenshots, Pin
Uwe Keim10-Dec-02 6:34
sitebuilderUwe Keim10-Dec-02 6:34 
GeneralRe: German Screenshots, Pin
Anonymous10-Dec-02 6:36
Anonymous10-Dec-02 6:36 
GeneralRe: German Screenshots, Pin
Uwe Keim10-Dec-02 7:00
sitebuilderUwe Keim10-Dec-02 7:00 

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

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