views:

1043

answers:

3

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..)!

+1  A: 

Is there a reason why you are not using msbuild? It can compile any visual studio project/solution in one command...

msbuild.exe <solution filename>
Nathan Reed
+1  A: 

Just use MSBuild passing in the Solution or Project file.

The best way to use msbuild is via the Visual Studio Command Prompt, sets all the environment variables for you. This command setup is also available in the SDK.

benPearce
+5  A: 

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.

Scott Weinstein
Scott, thanks for the Tip.. it definitely helped however there are still errors in my version. If I run msbuild it builds the executable just fine, but if I manually try and run the commands the executable that gets built throws an error when trying to locate resources with the ResourceManager..!?!
Miky Dinescu