- Download the dotnet 8 SDK binaries (.0.405-windows-x64-binaries)
- Locate dotnet.exe (dotnet-sdk-8.0.405-win-x64\dotnet.exe)
- Locate the CSharp compiler (dotnet-sdk-8.0.405-win-x64\sdk\8.0.405\Roslyn\bincore\csc.dll)
- Write a simple single-file program and lets name it test.cs, for example.
using System;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Test");
return;
}
}
- Compile using the following script Note that assemblies are located at "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
Running test.exe does not run the application and returns an exception.
I've found out that compiling the program will not produce a 'self-contained' app. (an app that has the dotnet runtime embedded into it or inside the same directory). Is there a missing step that will make the application use the SDK residing inside the same directory? Please also note that I copied mscorlib.dll and others into the same directory but an exception is thrown complaining about System.Runtime.dll
Thank you.
- Download the dotnet 8 SDK binaries (https://dotnet.microsoft/en-us/download/dotnet/thank-you/sdk-8.0.405-windows-x64-binaries)
- Locate dotnet.exe (dotnet-sdk-8.0.405-win-x64\dotnet.exe)
- Locate the CSharp compiler (dotnet-sdk-8.0.405-win-x64\sdk\8.0.405\Roslyn\bincore\csc.dll)
- Write a simple single-file program and lets name it test.cs, for example.
using System;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Test");
return;
}
}
- Compile using the following script Note that assemblies are located at "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
Running test.exe does not run the application and returns an exception.
I've found out that compiling the program will not produce a 'self-contained' app. (an app that has the dotnet runtime embedded into it or inside the same directory). Is there a missing step that will make the application use the SDK residing inside the same directory? Please also note that I copied mscorlib.dll and others into the same directory but an exception is thrown complaining about System.Runtime.dll
Thank you.
Share Improve this question edited Jan 31 at 6:35 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Jan 30 at 13:10 NiebieskiNiebieski 113 bronze badges1 Answer
Reset to default 1how to build a self-contained executable on Windows
We use dotnet publish
command to publish a self-contained application. You can run command like below in the directory of the root folder of the project. -r
is used to set the target runtime and -o
is used to define where the app is published to. You can use -f
to set the target framework version e.g. -f net8.0
, more options can be seen here.
dotnet publish -r win-x64 --self-contained true -o C:\Publish
What you used CSharp compiler csc.dll
is used for compiling C# source code into common intermediate language(CIL) code. It can produces .dll or .exe file but it doesn't produce a fully standalone executable file which includes the .NET runtime. Compilation is just one step in the process of creating a runnable application and this is why I introduce dotnet publish command here.