Command Line Compiling a Win Forms C# Application

霸气de小男生 提交于 2021-02-06 13:58:11

问题


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:


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.




回答2:


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>



回答3:


Just use MSBuild passing in the solution or project file.

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



来源:https://stackoverflow.com/questions/708029/command-line-compiling-a-win-forms-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!