67

I want to automate the build process for my C# solutions. How can I build C# solutions from the command line so that I don't have to deal with dependencies manually?

Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
alexfr
  • 1,161
  • 3
  • 12
  • 23
  • What makes you think that dependencies don't matter when you build on the cmd line? It is a helluvalot more manual, you'll have to type the full path name to the dependent assembly. – Hans Passant Oct 08 '10 at 15:37
  • I'm not sure you understood me correctly. All I want is to simply build a C# solution from command line in such a way that (almost) only the path to the solution file is needed and MSBuild seems to be the way to go. – alexfr Oct 08 '10 at 16:03

6 Answers6

75

For solutions you can use:

devenv /build Release Solution.sln

or

devenv /build Debug Solution.sln

Nate
  • 2,462
  • 1
  • 20
  • 28
  • 11
    This requires Visual Studio to be installed on the machine executing the build. msbuild is available with .net framework. – Jay Walker Nov 29 '12 at 15:10
  • 1
    +1. For an unknown reason, msbuild doesn't work on a brand new .sln file generated with `qmake -recursive -tp vc my.sln`. I had to open the solution with VS to get msbuild working. With devenv, I can automate the qmake and build process. Thanks – huysentruitw Apr 15 '13 at 07:01
  • I tried this in a batch file, but had to put the line `call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat"` on top of it. Then it worked. If you have a different edition of VS, you need to change the path to VsDevCmd.bat. – Matt Dec 03 '19 at 09:36
  • What if I want to specify output folder path? – Gulzar Jun 18 '20 at 14:19
40

if you open a visual studio command prompt from your start menu - then you can call MSBuild and give that either the .sln file or a specific .csproj file in order to build what you need

alternatively you can create a custom MSBuild file that takes care of the tasks.

one tip: make sure the version of MSBuild that you use is applicable to the target framework or tools version of the project

i.e. if you try and build a solution that was created in vs2010 with msbuild 3.5 then it will not recognise the 4.0 toolset of the project

stack72
  • 8,198
  • 1
  • 31
  • 35
  • I had to execute `vcvarsall.bat` before I could get `msbuild` to run from any old cli instance, once I did that it works like a charm! –  Nov 29 '12 at 15:13
  • 2
    You could instead open the "Developer Command Prompt": All Programs -> Microsoft Visual Studio -> Visual Studio Tools. – kroiz Sep 03 '13 at 08:00
22

Visual Studio project and solution files are also MSBuild build files.

You can simply run MSBuild against the solution/project file and it will build:

<path to>msbuild.exe <path to>solution/project file
Oded
  • 489,969
  • 99
  • 883
  • 1,009
18

msbuild YourSolution.sln

Steve Michelotti
  • 5,223
  • 1
  • 25
  • 30
1

Personally I'm a huge fan of Rake (yeah - I heard you when you said your c# solution)

Check it out: http://www.lostechies.com/blogs/derickbailey/archive/2009/09/23/albacore-a-suite-of-rake-build-tasks-for-net-solutions.aspx

Have fun - it made life a lot better for me!

Paul
  • 5,514
  • 2
  • 29
  • 38
1

you can use the c# (csc.exe) compiler directly:

Command-line building with csc.exe

STW
  • 44,917
  • 17
  • 105
  • 161