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

问题描述 投票:305回答:5

对于面向.NET Core 1.0的控制台应用程序项目,我无法弄清楚如何在构建期间输出.exe。项目在调试中运行良好。

我已经尝试发布该项目,但这也不起作用。有道理,因为.exe将是特定于平台的,但必须有一种方法。我的搜索只引用了使用project.json的旧版.Net Core版本。

无论何时我建立或发布,这都是我得到的。

build directory

.net-core
5个回答
379
投票

出于调试目的,您可以使用dll文件。你可以使用dotnet ConsoleApp2.dll运行它。如果要生成exe,则必须生成自包含的应用程序。

要生成自包含的应用程序(在Windows中为exe),您必须指定目标运行时(特定于您定位的OS)。

仅限Pre-.NET Core 2.0:首先,在csproj(list of supported rid)中添加目标运行时的运行时标识符:

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

从.NET Core 2.0开始不再需要上述步骤。

然后,在发布应用程序时设置所需的运行时:

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

32
投票

对于使用Visual Studio且希望通过GUI执行此操作的任何人,请参阅以下步骤:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here


11
投票

以下将在输出目录中生成

  • 所有包参考
  • 输出组件
  • bootstrapping exe

但不包含所有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>

我把它包裹在这里的样本https://github.com/SimonCropp/NetCoreConsole


1
投票

如果.bat文件可以接受,您可以创建一个与dll同名的bat文件(并将其放在同一个文件夹中),然后粘贴以下内容:

dotnet %0.dll %*

显然,这假设机器已安装.NET Core。

在没有'.bat'部分的情况下调用它。即:c:\>"path\to\program" -args blah(这个答案来自Chet的评论)


0
投票

这是我的hacky解决方法 - 生成一个控制台应用程序(.NET Framework),它读取自己的名称和args,然后调用dotnet [nameOfExe].dll [args]

当然,这假设dotnet安装在目标机器上。

这是代码,随意复制!

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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.