命令行编译 Win Forms C# 应用程序

发布于 2024-07-16 20:46:04 字数 2126 浏览 8 评论 0原文

我正在尝试创建一个脚本来从命令行编译 Windows 窗体 C# 2.0 项目(我知道,我知道..我正在重新发明轮子..再次..但如果有人知道答案,我将不胜感激它)。

该项目是一个标准的 Windows 窗体项目,具有一些资源并引用几个外部程序集。 这是文件列表:

    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

到目前为止,我已经确定了我需要的以下程序/工具:

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

这是到目前为止我的脚本:

    @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!!

最后,无论我如何旋转它,我都会从链接器中得到不同的错误,否则最终的可执行文件将无法运行 - 它会崩溃。

请帮助(MSDN 没有提供太多帮助..)!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-07-23 20:46:05

我喜欢为这类事情作弊。 获取一个工作项目并对其运行以下命令。

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

该命令的输出将向您显示 msbuild 用于编译该项目的命令,然后您可以根据需要进行修改。

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.

草莓味的萝莉 2024-07-23 20:46:05

您不使用 msbuild 有什么原因吗? 它可以用一个命令编译任何 Visual Studio 项目/解决方案...

msbuild.exe <solution filename>

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>
初雪 2024-07-23 20:46:05

只需使用 MSBuild 传入解决方案或项目文件即可。

使用 MSBuild 的最佳方法是通过 Visual Studio 命令提示符; 它为您设置所有必需的环境变量。 SDK 中也提供了此命令设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文