建立.NET Core控制台应用程序以输出EXE?

本文翻译自:Build .NET Core console application to output an EXE?

For a console application project targeting .NET Core 1.0, I cannot figure out how to get an .exe to output during build. 对于针对.NET Core 1.0的控制台应用程序项目,我无法弄清楚如何在构建过程中输出.exe。 Project runs fine in debug. 项目在调试中运行良好。

I've tried publishing the project but that does not work either. 我已经尝试发布该项目,但这也不起作用。 Makes sense since .exe would be platform-specific, but there must be a way. 因为.exe是特定于平台的,所以这是有道理的,但是必须有一种方法。 My searches have only turned up reference to older .Net Core versions that used project.json. 我的搜索只找到了使用project.json的.Net Core早期版本的参考。

Whenever I build or publish, this is all I get. 每当我构建或发布时,这就是我得到的全部。

建立目录


#1楼

参考:https://stackoom.com/question/2yvgv/建立-NET-Core控制台应用程序以输出EXE


#2楼

For debugging purposes, you can use the dll file. 出于调试目的,您可以使用dll文件。 You can run it using dotnet ConsoleApp2.dll . 您可以使用dotnet ConsoleApp2.dll运行它。 If you want to generate an exe, you have to generate a self-contained application. 如果要生成一个exe,则必须生成一个自包含的应用程序。

To generate a self-contained application (exe in windows), you must specify the target runtime (which is specific to the OS you target). 要生成独立的应用程序(Windows中的exe),必须指定目标运行时(特定于目标操作系统)。

Pre-.NET Core 2.0 only : First, add the runtime identifier of the target runtimes in the csproj ( list of supported rid ): 仅限于.NET Core 2.0之前的版本 :首先,在csproj( 受支持的rid列表 )中添加目标运行时的运行时标识符:

<PropertyGroup>
    <RuntimeIdentifiers>win10-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>

The above step is no longer required starting with .NET Core 2.0 . 从.NET Core 2.0开始,不再需要上述步骤

Then, set the desired runtime when you publish your application: 然后,在发布应用程序时设置所需的运行时:

dotnet publish -c Release -r win10-x64
dotnet publish -c Release -r ubuntu.16.10-x64

#3楼

The following will produce, in the output directory, 以下将在输出目录中产生:

  • all the package references 所有包装参考
  • the output assembly 输出组件
  • the bootstrapping exe 引导程序

But does not contain all netcore runtime assemblies 但不包含所有netcore运行时程序集

<PropertyGroup>
  <Temp>$(SolutionDir)\packaging\</Temp>
</PropertyGroup>

<ItemGroup>
  <BootStrapFiles Include="$(Temp)hostpolicy.dll;$(Temp)$(ProjectName).exe;$(Temp)hostfxr.dll;"/>
</ItemGroup>

<Target Name="GenerateNetcoreExe"
        AfterTargets="Build"
        Condition="'$(IsNestedBuild)' != 'true'">
  <RemoveDir Directories="$(Temp)" />
  <Exec
    ConsoleToMSBuild="true"
    Command="dotnet build $(ProjectPath) -r win-x64 /p:CopyLocalLockFileAssemblies=false;IsNestedBuild=true --output $(Temp)" >
    <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
  </Exec>
  <Copy
    SourceFiles="@(BootStrapFiles)"
    DestinationFolder="$(OutputPath)"
  />

</Target>

i wrapped it up in a sample here https://github.com/SimonCropp/NetCoreConsole 我将其包装在此处的示例中https://github.com/SimonCropp/NetCoreConsole


#4楼

Here's my hacky workaround - generate a Console Application (.NET Framework) that reads its own name and args, then calls dotnet [nameOfExe].dll [args] 这是我的解决方法-生成一个控制台应用程序(.NET Framework),该程序读取其自己的名称和参数,然后调用dotnet [nameOfExe].dll [args]

Of course this assumes that dotnet is installed on the target machine. 当然,这假定在目标计算机上安装了dotnet。

Here's the code, feel free to copy! 这是代码,请随时复制!

using System;
using System.Diagnostics;
using System.Text;

namespace dotNetLauncher
{
    class Program
    {
        /*
            If you make .net core apps, they have to be launched like dotnet blah.dll args here
            This is a convenience exe that launches .net core apps via name.exe
            Just rename the output exe to the name of the .net core dll you wish to launch
        */
        static void Main(string[] args)
        {
            var exePath = AppDomain.CurrentDomain.BaseDirectory;
            var exeName = AppDomain.CurrentDomain.FriendlyName;
            var assemblyName = exeName.Substring(0, exeName.Length - 4);
            StringBuilder passInArgs = new StringBuilder();
            foreach(var arg in args)
            {
                bool needsSurroundingQuotes = false;
                if (arg.Contains(" ") || arg.Contains("\""))
                {
                    passInArgs.Append("\"");
                    needsSurroundingQuotes = true;
                }
                passInArgs.Append(arg.Replace("\"","\"\""));
                if (needsSurroundingQuotes)
                {
                    passInArgs.Append("\"");
                }

                passInArgs.Append(" ");
            }
            string callingArgs = $"\"{exePath}{assemblyName}.dll\" {passInArgs.ToString().Trim()}";

            var p = new Process
            {
                StartInfo = new ProcessStartInfo("dotnet", callingArgs)
                {
                    UseShellExecute = false
                }
            };

            p.Start();
            p.WaitForExit();
        }
    }
}

#5楼

If a .bat file is acceptable, you can create a bat file with the same name as the dll (and place it in the same folder), then paste in the following content: 如果可接受.bat文件,则可以创建一个与dll名称相同的bat文件(并将其放置在同一文件夹中),然后粘贴以下内容:

dotnet %0.dll %*

Obviously, this assumes that the machine has .NET Core installed. 显然,这是假定计算机已安装.NET Core。

Call it without the '.bat' part. 不带“ .bat”部分调用它。 ie: c:\\>"path\\to\\program" -args blah (this answer is derived from Chet's comment) 即: c:\\>"path\\to\\program" -args blah (此答案来自Chet的评论)


#6楼

UPDATE(31-OCT-2019) For anyone that wants to do this via GUI and: UPDATE(31-OCT-2019)对于任何想要通过GUI和执行以下操作的人:

  • Is Using Visual Studio 2019 正在使用Visual Studio 2019
  • Has .NET Core 3.0 installed (included in latest version of Visual Studio 2019) 已安装.NET Core 3.0 (包含在最新版本的Visual Studio 2019中)
  • Wants to generate a SINGLE FILE 想要生成一个文件

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Note 注意

Notice the large file size for such a small application 注意这么小的应用程序的文件很大 在此处输入图片说明

You can add the "PublishTrimmed" property. 您可以添加“ PublishTrimmed”属性。 The application will only include components that are used by the application. 该应用程序将仅包括该应用程序使用的组件。 CAUTION: don't do this if you are using reflection 注意:如果您使用反射,请不要这样做

在此处输入图片说明

Publish again 重新发布

在此处输入图片说明


Previous Post 上一篇

For anyone that's using Visual Studio and wants to do this via GUI, see the steps below: 对于正在使用Visual Studio并希望通过GUI进行操作的任何人,请参见以下步骤:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值