✅ 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
Buddy
Buddy4d ago
$singlefile
MODiX
MODiX4d ago
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:
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
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
Jonathan Wilson
Jonathan WilsonOP4d ago
I have added these lines to my cxproj file:
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
but its not combining my exe file and dlls into one binary and the exe file still needs the dll file.
Buddy
Buddy4d ago
It can be that your IDE overrides the settings. Try doing it by console as written above.
Pobiega
Pobiega4d ago
(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
Jonathan Wilson
Jonathan WilsonOP4d ago
I have some legacy x86 native code I need to work with. But I got it working now.
Pobiega
Pobiega4d ago
Ah, ooft For posterity, what was the problem and what was the fix?
Jonathan Wilson
Jonathan WilsonOP4d ago
I just had to select "publish" in Visual Studio and set some settings there. And it built the exe I wanted.
Pobiega
Pobiega4d ago
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

Did you find this page helpful?