C
C#10mo ago
Jānis

Publish to single working .exe

Hi, almost complete beginner with c#, and I'm not sure, what I need to do, to solve it. Previously I made a very small app with target framework, I think, 4.8. I simply built the solution, took the .exe file from the solution and could distribute it to others, it simply worked. [1] Current project is .net 7.0. When building it, it builds also multiple files [2]. However, I can't simply take the .exe. It doesn't work without the other four files. I tried costura, multiple different .csproj options, but none of them work, if I only want to take the .exe and run it from another computer. The app is very very simple, like ~ 350 loc... I managed to bring it up to 150 MB with embeded dependencies I assume, it still didn't work. What I should look into, to make it work as single .exe if I want to run it myself? Another issue I encountered, even if I give all of the files to another person, he didn't have 7.0 runtime, is there some kind of version that should be available by default on windows, or I need to check this with IT? Best regards!
No description
No description
21 Replies
Angius
Angius10mo ago
$singlefile
MODiX
MODiX10mo 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. 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
Angius
Angius10mo ago
Far as versions go, every Windows installation is guaranteed to have .NET Framework Not .NET Thankfully, .NET apps can be deployed as self-contained, meaning, they package the runtime with them
Jānis
JānisOP10mo ago
Maybe the problem is somewhere else. I tried different approaches for the singlefile. The ,exe grows larger, using self contained even to 150 mb, but if I copy it separately out of this folder, it doesn't work. :/
No description
Angius
Angius10mo ago
Did you try this exact command? Preferably, to some other output directory?
dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -o ./artifacts
dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -o ./artifacts
cd ./artifacts
cd ./artifacts
ls
ls
Jānis
JānisOP10mo ago
I used .csproj settings, but I tried to afterwards close studio, delete the publish folders, run this command from cmd within proj folder. It generated the same result as with csproj settings :/ I'll try with the artifacts folder
No description
Jānis
JānisOP10mo ago
With this I get the largest file, that I had got also previously It opens immediately, if I open from this folder. If I copy only the exe elsewhere - can't open the exe then
No description
Angius
Angius10mo ago
Weird The .pdb file is just debugging symbols, so this one can be safely deleted Not sure why the other DLLs don't get rolled into the exe tho
Jānis
JānisOP10mo ago
I'm not using anything particularly exotic using System; using System.Collections.Generic; using System.Windows; using Microsoft.Win32; using Microsoft.VisualBasic.FileIO; using System.IO;
Angius
Angius10mo ago
Add -p:IncludeNativeLibrariesForSelfExtract=true
Jānis
JānisOP10mo ago
SUCCESS!
Angius
Angius10mo ago
Nice
Jānis
JānisOP10mo ago
Can I try adding this property in csproj with the previous settings, and try to get the <9MB file size instead of this self contained monstrosity? 😄 I'm completely clueless with these properties, how the runtimes e.t.c. work
Angius
Angius10mo ago
Yeah, you can just have it in the csproj Under 9 MB... not sure, maybe if you release it as framework-dependent Or you can try trimming
Jānis
JānisOP10mo ago
framework dependant will be fine. Trimming was conflicting for me with using wpf. The target computers have specific frameworks due to other software being used, I'll just have to inquire, which specific they have at least, I wasn't having any issues distributing the previous 4.8, that was generated using the basic settings for wpf .net app and again huge thanks, I wouldn't have stumbled upon this option by myself
Angius
Angius10mo ago
--self-contained false to create a framework-dependent version, then
MODiX
MODiX10mo ago
=> $singlefile
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Jānis
JānisOP10mo ago
Thank you for your comments! My main problem was that I was not understanding why in one project 100 kB .exe was working, and in another project it wasn't. At least that is solved, I got it working with the size of 155 MB, played around with the settings, got it back runtime dependant but with 100 kB. I don't need newest runtimes, dependencies e.t.c. I just need something that works. 😄 And if possible, with the relatively small size. I'll talk to IT, but if necessary convert also this new project back to 4.8, if it means, I can distribute the small exe to necessary people.
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Lex Li
Lex Li10mo ago
"why in one project 100 kB .exe was working" simply because you were cheated. There are a few MB of bits there on each Windows called .NET Framework that help make this 100kB .exe work. .NET 7 or any other .NET releases were designed differently, so now you need to distribute your programs differently. Young generation of developers might never read how terrible it was before 2010 to distribute your several kB programs with a big .NET Framework installer. Well, only old people keep that horrible memory.
Want results from more Discord servers?
Add your server