.NET Core 6.0独立二进制编译

3th*_*1ll 3 c# .net-core

我正在尝试创建一个独立的 .NET Core 6.0 二进制文件,并将所有依赖项打包在一个.exe文件中。

我遇到的问题是,虽然二进制文件编译正常,但它依赖于位于 Release/Debug 文件夹中的 DLL。

我尝试过从命令行编译

dotnet publish --standalone
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,我只是遇到了类似的问题,加载更多 DLL,并且二进制文件本身大小相同,需要位于该文件夹中才能运行。

我正在寻找的东西是否可能,如果可以,如何实现?dotnet到目前为止,我已经尝试过 Visual Studio、 cli 和 Rider。

有许多旧的解决方案提到了诸如此类的解决方案,ilmerge但这似乎早已被弃用并且不再维护。

-- 为未来的我编辑:

最终的解决方案看起来像这样,感谢下面安德鲁的回答。

我的最终 project.csproj 文件看起来像这样基于MS Docs

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishSingleFile>true</PublishSingleFile>
    <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

通过 Visual Studio GUI 发布或:

dotnet publish -c release -r win-x64
Run Code Online (Sandbox Code Playgroud)

And*_*ent 5

我建议查看https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file,希望它是最新的。我相信关键位<PublishSingleFile>true</PublishSingleFile>在 .csproj 中。