Make Windows Forms exe smaller?

Hello!

I wrote a Windows Forms app in .NET 7. The EXE that is created upon publishing should be standalone (including the framework) and as small as possible. I currently have about 1200 LoC, which brings me to just under 150MB.

For .net7 console applications, I was used to setting PublishTrimmed to true, and I also always used these settings:

<TrimmerRootAssembly Include="assemblyName" TrimMode="copy" />

and

<link>

<assembly fullname="assemblyName" preserve="all"/>

</link>

These settings brought me to under 15 mb for a console application (incl. runtime)

Unfortunately, when publishing with the Windows Forms app (with PublishTrimmed set to true), I got the message: "Windows Forms is not supported or recommended with cropping enabled."

I hope for your help to make the exe smaller.. 150mb is pretty extreme for such a small application with no dependencies.

Maybe you have some tips, unfortunately I haven't found anything online that helps^^

Best regards,

Oli

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
10 months ago

Trimming is problematic in applications that work with Reflection or COM-Marshalling, because during the analysis process it is difficult to assess what can be done or not. Windows Forms is particularly affected at the point, as it builds on COM-Marshalling under the hood. A possible solution in place is the use of COM wrappers (see e.g. WinFormsComInterop).

In addition, you should use the Microsoft article Prepare libraries for trimming work.

A guarantee that you can do it, but there is not. Pages of the .NET team here) there is no official support yet. This Epic provides an overview of the current development status: #4649 Make WinForms trim compatible.

Another few other options you could try/start:

  • Deploye the app in release mode.
  • Go this list to features. Surely you can exclude some of them.
  • See if there are files in the project that can be explicitly excluded and mark them accordingly.
  • Compress media files (including external resources) that you may use.
  • With a packer/compressor, you can compress your executable again (see, for example, NeoLite or UPX).
  • Something that would have to be tested in case-by-case: a division into assemblies and a partial trimming.
CosmiqUser
10 months ago
Reply to  regex9

@
I also praise you.
How can you be so smart? Respect. I want to go. You are now my model now. Officially, I’m also a fan of you.

Ginpanse
10 months ago

then write you nen assembler code;) 150mb is not