Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 13 Dec 2002
Shows how to build solution files from command line or via contextmenu item.
builditemincontextmenu.zip
vsbuilderhelper.zip
VsBuilderHelper
VsBuilderHelper.csproj.user
VsBuilderHelper.csproj.vspscc
using System;

namespace VsBuilderHelper
{
	/// <summary>
	/// Summary description for Builder.
	/// </summary>
	public class Builder
	{
		#region members
		public string errorLog;
		#endregion

		#region constructors		
		public Builder()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#endregion

		#region public interface
		[STAThread]
		public static int Main(string[] args)
		{
			if (args.GetLength(0) == 0)
			{
				System.Console.WriteLine("Invalid swtiches used. Syntax is ");
				System.Console.WriteLine(@"VsBuilderHelper.exe c:\mysolution.sln debug");
				System.Console.WriteLine("Hit enter to exit...");
				System.Console.Read();
				return 1;
			}
			Builder b = new Builder();
			return b.Build(args.GetValue(0).ToString(), args.GetValue(1).ToString());
		}
		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;
			// 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			
			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);
			// return process exit code
			return exitCode;
		}
		#endregion

		#region internals
		#endregion

		#region events
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Chris Richner
Software Developer (Senior) Zeit AG
Switzerland Switzerland
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 - now C#, WCF DS (OData), WF, WPF
Interests
  • family & friends
  • chilaxing ,)
  • coding
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 14 Dec 2002
Article Copyright 2002 by Chris Richner
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid