COMPILING C# USING BATCH SCRIPTS

If you have installed .NET on your computer then you already have everything you need to compile C#. And starting a heavy IDE for a small project seems a bit overkill. Lets look at how we can get building C# applications using command line and a simple batch script.

First lets find out what versions we have installed of dot net, open a command line shell and type: "dir /AD C:\WINDOWS\Microsoft.NET\Framework\". The folders with versions numbers are the different dot net versions installed, and each one of them has a corresponding compiler!

C:\>dir /AD C:\WINDOWS\Microsoft.NET\Framework\

 Directory of C:\WINDOWS\Microsoft.NET\Framework

11/04/2014  08:50 AM    <DIR>          .
11/04/2014  08:50 AM    <DIR>          ..
11/04/2014  07:41 AM    <DIR>          v1.0.3705
11/04/2014  07:37 AM    <DIR>          v1.1.4322
11/04/2014  08:46 AM    <DIR>          v2.0.50727
05/05/2014  12:47 PM    <DIR>          v4.0.30319
               0 File(s)              0 bytes
               6 Dir(s)  123,224,203,264 bytes free

The compiler filename is csc.exe, using /? parameter provides a list of arguments.

C:\>C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe /?
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

                        Visual C# 2010 Compiler Options

                        - OUTPUT FILES -
/out:<file>                    Specify output file name (defaul....
/target:exe                    Build a console executable (defa...
/target:winexe                 Build a Windows executable (Short....
/target:library                Build a library (Short form: /t:l>..
...

PROJECT FILES

Lets create a small project by creating a folder, in this case: /helloproject/. And we start by adding our C# code files, and a build.bat file. In the build file below you will notice the argument to csc.exe is "/recurse:*.cs" this means process all c# files recursivly.

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello, World!");
        }
    }
}

Our build file is set to build for a specific version of dot net (see DOTNET_VER variable), and has a fixed name of executable to build (/out:helloworld.exe). You might want to change that to fit your project.

Almost there, the last thing to sort is the path. Since multiple versions is installed there would be a name conflict by setting environment path to all, so we do it in the build file.

build.bat:

@ECHO OFF
SET SCRIPT_VERSION=csharp commandline build  (2012-07-27 TMB)

REM --------------------------------------------------------------------------
REM                     DOT NET VERSIONS INSTALLED
REM --------------------------------------------------------------------------

SET DOTNET_VER10=v1.0.3705
SET DOTNET_VER11=v1.1.4322
SET DOTNET_VER20=v2.0.50727
SET DOTNET_VER30=v3.0
SET DOTNET_VER35=v3.5
SET DOTNET_VER40=v4.0.30319

REM --------------------------------------------------------------------------
REM                 SELECT YOUR VERSION TO BUILD HERE:
REM --------------------------------------------------------------------------

SET DOTNET_VER=%DOTNET_VER40%

REM --------------------------------------------------------------------------
REM                 ALL KNOWN VERSIONS ARE INSTALLED HERE
REM --------------------------------------------------------------------------

SET DOTNET_PATH=C:\WINDOWS\Microsoft.NET\Framework\%DOTNET_VER%


ECHO %SCRIPT_VERSION%
ECHO [BUILD:INIT]
ECHO.
ECHO  -- Build file uses ...........: Microsoft.NET Framework %DOTNET_VER%

ECHO  -- Dot net framework path ....: %DOTNET_PATH%

For /F "Delims=" %%I In ('echo %PATH% ^| find /C /I "%DOTNET_PATH%"') Do set PATH_EXISTS=%%I 2>Nul
If %PATH_EXISTS%==0 (
  ECHO  -- Updating environment PATH...
  set PATH=%DOTNET_PATH%;%PATH%
)

REM --------------------------------------------------------------------------
REM                 ACTUAL BUILD IS DONE HERE:
REM --------------------------------------------------------------------------

ECHO.
ECHO.
ECHO [BUILD:START]
ECHO.

csc.exe /out:helloworld.exe /recurse:*.cs

ECHO.
ECHO.
ECHO [BUILD:ENDED]
pause

Running the build file (notice the pause, remove this if you are running from command line).

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\helloproject>build.bat
csharp commandline build  (2012-07-27 TMB)
[BUILD:INIT]

 -- Build file uses ...........: Microsoft.NET Framework v4.0.30319
 -- Dot net framework path ....: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
 -- Updating environment PATH...


[BUILD:START]

Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.



[BUILD:ENDED]
Press any key to continue . . .

C:\helloproject>

That was easier than it sounds!

Download example files used:

helloproject.zip
Site maintained by tommy@trapv.com.
Page generated in: 0.017 seconds.
Memory usage: 427272 bytes (peak: 467080 bytes)