Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#

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 230.3K   1.1K   31  
Shows how to build solution files from command line or via contextmenu item.
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)


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