Click here to Skip to main content
12,401,324 members (30,383 online)
Click here to Skip to main content

Stats

198.7K views
1K downloads
31 bookmarked
Posted

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

, 13 Dec 2002 CPOL
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)

Share

About the Author

Chris Richner
Software Developer (Senior) Zeit AG
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 - now C#, Azure PaaS, Identity, OWIN, OData, Web Api
Interests
  • family & friends
  • chilaxing ,)
  • coding

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 14 Dec 2002
Article Copyright 2002 by Chris Richner
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid