You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows平台构建.NET自包含可执行文件的问题求助

Windows下构建.NET自包含可执行文件遇到的问题

操作步骤

  1. 下载.NET 8 SDK二进制文件
  2. 定位dotnet.exe,路径为dotnet-sdk-8.0.405-win-x64\dotnet.exe
  3. 找到C#编译器csc.dll,路径为dotnet-sdk-8.0.405-win-x64\sdk\8.0.405\Roslyn\bincore\csc.dll
  4. 编写简单单文件程序test.cs,代码如下:
using System;

class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Test");
        return;
    }
}
  1. 使用以下脚本编译(引用程序集位于packs\Microsoft.NETCore.App.Ref\8.0.8\ref\net8.0):
set sdk_dotnet=dotnet-sdk-8.0.405-win-x64\dotnet.exe
set sdk_dotnet_csc=dotnet-sdk-8.0.405-win-x64\sdk\8.0.405\Roslyn\bincore\csc.dll
%sdk_dotnet% %sdk_dotnet_csc%^
 /r:Microsoft.CSharp.dll^
 /r:Microsoft.VisualBasic.Core.dll^
 /r:Microsoft.VisualBasic.dll^
 /r:Microsoft.Win32.Primitives.dll^
 /r:Microsoft.Win32.Registry.dll^
 /r:mscorlib.dll^
 /r:netstandard.dll^
 /r:System.AppContext.dll^
 /r:System.Buffers.dll^
 /r:System.Collections.Concurrent.dll^
 /r:System.Collections.dll^
 /r:System.Collections.Immutable.dll^
 /r:System.Collections.NonGeneric.dll^
 /r:System.Collections.Specialized.dll^
 /r:System.ComponentModel.Annotations.dll^
 /r:System.ComponentModel.DataAnnotations.dll^
 /r:System.ComponentModel.dll^
 /r:System.ComponentModel.EventBasedAsync.dll^
 /r:System.ComponentModel.Primitives.dll^
 /r:System.ComponentModel.TypeConverter.dll^
 /r:System.Configuration.dll^
 /r:System.Console.dll^
 /r:System.Core.dll^
 /r:System.Data.Common.dll^
 /r:System.Data.DataSetExtensions.dll^
 /r:System.Data.dll^
 /r:System.Diagnostics.Contracts.dll^
 /r:System.Diagnostics.Debug.dll^
 /r:System.Diagnostics.DiagnosticSource.dll^
 /r:System.Diagnostics.FileVersionInfo.dll^
 /r:System.Diagnostics.Process.dll^
 /r:System.Diagnostics.StackTrace.dll^
 /r:System.Diagnostics.TextWriterTraceListener.dll^
 /r:System.Diagnostics.Tools.dll^
 /r:System.Diagnostics.TraceSource.dll^
 /r:System.Diagnostics.Tracing.dll^
 /r:System.dll^
 /r:System.Drawing.dll^
 /r:System.Drawing.Primitives.dll^
 /r:System.Dynamic.Runtime.dll^
 /r:System.Formats.Asn1.dll^
 /r:System.Formats.Tar.dll^
 /r:System.Globalization.Calendars.dll^
 /r:System.Globalization.dll^
 /r:System.Globalization.Extensions.dll^
 /r:System.IO.Compression.Brotli.dll^
 /r:System.IO.Compression.dll^
 /r:System.IO.Compression.FileSystem.dll^
 /r:System.IO.Compression.ZipFile.dll^
 /r:System.IO.dll^
 /r:System.IO.FileSystem.AccessControl.dll^
 /r:System.IO.FileSystem.dll^
 /r:System.IO.FileSystem.DriveInfo.dll^
 /r:System.IO.FileSystem.Primitives.dll^
 /r:System.IO.FileSystem.Watcher.dll^
 /r:System.IO.IsolatedStorage.dll^
 /r:System.IO.MemoryMappedFiles.dll^
 /r:System.IO.Pipes.AccessControl.dll^
 /r:System.IO.Pipes.dll^
 /r:System.IO.UnmanagedMemoryStream.dll^
 /r:System.Linq.dll^
 /r:System.Linq.Expressions.dll^
 /r:System.Linq.Parallel.dll^
 /r:System.Linq.Queryable.dll^
 /r:System.Memory.dll^
 /r:System.Net.dll^
 /r:System.Net.Http.dll^
 /r:System.Net.Http.Json.dll^
 /r:System.Net.HttpListener.dll^
 /r:System.Net.Mail.dll^
 /r:System.Net.NameResolution.dll^
 /r:System.Net.NetworkInformation.dll^
 /r:System.Net.Ping.dll^
 /r:System.Net.Primitives.dll^
 /r:System.Net.Quic.dll^
 /r:System.Net.Requests.dll^
 /r:System.Net.Security.dll^
 /r:System.Net.ServicePoint.dll^
 /r:System.Net.Sockets.dll^
 /r:System.Net.WebClient.dll^
 /r:System.Net.WebHeaderCollection.dll^
 /r:System.Net.WebProxy.dll^
 /r:System.Net.WebSockets.Client.dll^
 /r:System.Net.WebSockets.dll^
 /r:System.Numerics.dll^
 /r:System.Numerics.Vectors.dll^
 /r:System.ObjectModel.dll^
 /r:System.Reflection.DispatchProxy.dll^
 /r:System.Reflection.dll^
 /r:System.Reflection.Emit.dll^
 /r:System.Reflection.Emit.ILGeneration.dll^
 /r:System.Reflection.Emit.Lightweight.dll^
 /r:System.Reflection.Extensions.dll^
 /r:System.Reflection.Metadata.dll^
 /r:System.Reflection.Primitives.dll^
 /r:System.Reflection.TypeExtensions.dll^
 /r:System.Resources.Reader.dll^
 /r:System.Resources.ResourceManager.dll^
 /r:System.Resources.Writer.dll^
 /r:System.Runtime.CompilerServices.Unsafe.dll^
 /r:System.Runtime.CompilerServices.VisualC.dll^
 /r:System.Runtime.dll^
 /r:System.Runtime.Extensions.dll^
 /r:System.Runtime.Handles.dll^
 /r:System.Runtime.InteropServices.dll^
 /r:System.Runtime.InteropServices.JavaScript.dll^
 /r:System.Runtime.InteropServices.RuntimeInformation.dll^
 /r:System.Runtime.Intrinsics.dll^
 /r:System.Runtime.Loader.dll^
 /r:System.Runtime.Numerics.dll^
 /r:System.Runtime.Serialization.dll^
 /r:System.Runtime.Serialization.Formatters.dll^
 /r:System.Runtime.Serialization.Json.dll^
 /r:System.Runtime.Serialization.Primitives.dll^
 /r:System.Runtime.Serialization.Xml.dll^
 /r:System.Security.AccessControl.dll^
 /r:System.Security.Claims.dll^
 /r:System.Security.Cryptography.Algorithms.dll^
 /r:System.Security.Cryptography.Cng.dll^
 /r:System.Security.Cryptography.Csp.dll^
 /r:System.Security.Cryptography.dll^
 /r:System.Security.Cryptography.Encoding.dll^
 /r:System.Security.Cryptography.OpenSsl.dll^
 /r:System.Security.Cryptography.Primitives.dll^
 /r:System.Security.Cryptography.X509Certificates.dll^
 /r:System.Security.dll^
 /r:System.Security.Principal.dll^
 /r:System.Security.Principal.Windows.dll^
 /r:System.Security.SecureString.dll^
 /r:System.ServiceModel.Web.dll^
 /r:System.ServiceProcess.dll^
 /r:System.Text.Encoding.CodePages.dll^
 /r:System.Text.Encoding.dll^
 /r:System.Text.Encoding.Extensions.dll^
 /r:System.Text.Encodings.Web.dll^
 /r:System.Text.Json.dll^
 /r:System.Text.RegularExpressions.dll^
 /r:System.Threading.Channels.dll^
 /r:System.Threading.dll^
 /r:System.Threading.Overlapped.dll^
 /r:System.Threading.Tasks.Dataflow.dll^
 /r:System.Threading.Tasks.dll^
 /r:System.Threading.Tasks.Extensions.dll^
 /r:System.Threading.Tasks.Parallel.dll^
 /r:System.Threading.Thread.dll^
 /r:System.Threading.ThreadPool.dll^
 /r:System.Threading.Timer.dll^
 /r:System.Transactions.dll^
 /r:System.Transactions.Local.dll^
 /r:System.ValueTuple.dll^
 /r:System.Web.dll^
 /r:System.Web.HttpUtility.dll^
 /r:System.Windows.dll^
 /r:System.Xml.dll^
 /r:System.Xml.Linq.dll^
 /r:System.Xml.ReaderWriter.dll^
 /r:System.Xml.Serialization.dll^
 /r:System.Xml.XDocument.dll^
 /r:System.Xml.XmlDocument.dll^
 /r:System.Xml.XmlSerializer.dll^
 /r:System.Xml.XPath.dll^
 /r:System.Xml.XPath.XDocument.dll^
 /r:WindowsBase.dll^
 test.cs

遇到的问题

编译后运行test.exe无法启动,抛出System.Runtime.dll相关异常。目前的方式无法生成自包含应用(将.NET运行时嵌入或放在同一目录),即使把mscorlib.dll等文件复制到同一目录也无效。请问如何让应用使用同一目录下的SDK?


问题根源与解决步骤

1. 直接复制引用程序集无效的原因

你引用的Microsoft.NETCore.App.Ref里的程序集是引用程序集(Reference Assemblies),仅包含元数据,没有实际运行代码,无法支撑程序执行。真正的运行时程序集位于dotnet-sdk-8.0.405-win-x64\shared\Microsoft.NETCore.App\8.0.11(版本号可能随SDK更新变化)目录下。

2. 官方推荐的自包含应用构建方式

不建议直接调用csc.dll编译,使用.NET SDK自带的dotnet publish命令是最可靠的方案:

步骤1:创建项目文件

在test.cs所在目录创建Test.csproj文件,内容如下:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishTrimmed>false</PublishTrimmed> <!-- 可选,需减小体积可设为true -->
  </PropertyGroup>
</Project>

步骤2:执行发布命令

打开命令行进入项目目录,运行:

dotnet-sdk-8.0.405-win-x64\dotnet.exe publish -c Release

发布完成后,自包含的test.exe会生成在bin\Release\net8.0\win-x64\publish目录下,该目录包含所有运行时依赖,可直接在未安装.NET的Windows机器上运行。

3. 手动调用csc编译的方案(不推荐)

若坚持手动编译,需完成以下操作:

  • 将引用的程序集替换为shared\Microsoft.NETCore.App\8.0.11下的真实运行时程序集
  • 添加/target:exe参数指定输出类型
  • 添加/runtimeconfig参数,指定运行时配置文件(可从SDK目录复制runtimeconfig.json并修改)
  • 复制所有必要的运行时文件到输出目录,包括coreclr.dll、hostfxr.dll等宿主文件,以及所有依赖的.NET程序集

此方式繁琐且易出错,官方不推荐使用,优先选择dotnet publish。

内容的提问来源于stack exchange,提问作者Niebieski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 16:44:53