✅ How do I build a standalone C# exe?
What do I need to set in Visual Studio so that VS will build me an exe with all my dependencies (but not the actual base .NET runtime files) linked in?
9 Replies
$singlefile
dotnet publish -c Release -r <runtime identifier> -p:PublishSingleFile=true
Use of -p:PublishSingleFile=true
implies --self-contained true
. Add --self-contained false
to publish as runtime-dependent.
-r RID
and -p:PublishSingleFile=true
can be moved to .csproj as the following properties:but to target multiple RIDs, you have to use dotnet publish
with the -r
option for each RID.
You can also add -p:IncludeNativeLibrariesForSelfExtract=true
to include native libraries (like Common Language Runtime dlls) in the output executable, but be aware of drawbacks and consider using an installer framework instead of that property with PublishSingleFile.
You might want to instead publish your application compiled Ahead Of Time to native code, see $nativeaot for examples.
Single file publishing | Runtime Identifier (RID) catalog | dotnet publish
I have added these lines to my cxproj file:
but its not combining my exe file and dlls into one binary and the exe file still needs the dll file.
It can be that your IDE overrides the settings.
Try doing it by console as written above.
(but not the actual base .NET runtime files)Then you shouldn't do self contained self-contained means "bundle the .NET runtime with my app" Note that we assume you are talking about modern .NET here x86 is an unusual RID
I have some legacy x86 native code I need to work with.
But I got it working now.
Ah, ooft
For posterity, what was the problem and what was the fix?
I just had to select "publish" in Visual Studio and set some settings there.
And it built the exe I wanted.
Right, so it was VS overriding the project with its publish settings as buddy thought
Glad you got it fixed!
/close
the thread if you have no further questions