C# – Command Line Compiling a Win Forms C# Application

.net-2.0ccmdcompiler-constructionnet

I'm trying to create a script to compile an Windows Forms C# 2.0 project from the command line (I know, I know.. I'm reinventing the wheel.. again.. but if somebody knows the answer, I'd appreciate it).

The project is a standard Windows Forms project that has some resources and references a couple external assemblies. Here is a list of the files:

    Program.cs                  // no need to expand on this on :)
    frmMain.cs                  // this is a typical C# windows forms file
    frmMain.designer.cs         //  .. and the designer code
    frmMain.resx                //  .. and the resource file
    MyClasses.cs                // this contains a couple classes
    Properties\AssemblyInfo.cs        // the Properties folder -- again pretty standard 
    Properties\Resources.Designer.cs
    Properties\Resources.resz
    Properties\Settings.Designer.cs
    Properties\Settings.settings
    References\AnAssembly.dll   // an assmebly that I reference from this application

So far I've identified the following programs/tools that I would need:

    csc.exe      //  the C# compiler
    al.exe       //  the assembly linker
    resgen.exe   //  the resource compiler

And this is my script so far:

    @echo off
    set OUT=Out
    set AL=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\al.exe
    set RESGEN="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\resgen.exe"
    set COMPILER=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe

    echo.
    echo Compiler: %COMPILER%
    echo.

    if "%1"=="/help" goto help

    :start
    echo Starting...

    set REFERENCES=.\References\AReferencedll
    set SRCFILES=Program.cs frmMain.cs frmMain.designer.cs MyClasses.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs

    del /Q %OUT%\*

    %RESGEN% /compile frmMain.resx,%OUT%\frmMain.resources 

    cd Properties

    %RESGEN% /compile Resources.resx,..\%OUT%\Resources.resources

    cd ..

    %COMPILER% /target:module /out:%OUT%\app.module %SRCFILES% /reference:%REFERENCES%

    %AL% %OUT%\app.module /embed:%OUT%\frmMain.resources /target:winexe /out:%OUT%\app.exe /main:App.Program.Main

    goto done
    :error
    echo Ooooops!

    :done
    echo Done!!

In the end, no matter how I spin it I get different errors from the linker, or the final executable will just not run – it crashes.

Please help (MSDN didn't help too much..)!

Best Answer

I like to cheat for these kinds of things. Take a working project and run the following command on it

msbuild Project.csproj /t:rebuild /clp:ShowCommandLine

The output of that will show you the command msbuild uses to compile the project, which you can then take and modify as you like.