Home c# How to compile all the projects of a solution in a single...

How to compile all the projects of a solution in a single .EXE?




There solyushen (Solution). It 4 project. One major, three minor. Main project uses a type of secondary and vice versa. In short they are all intertwined. How to compile all this into a single .EXE file for distribution?

Because it seems that not everyone understood what it was about, then try to supplement your question.

Here’s an example:
In Solution Explorer shows that in the Solution adds two project. App2 App1 project depends on the project. App1 added in the References.

It is also prescribed appropriate dependency.

If you simply compile the Solution, then the output will get two files – App2.exe and App1.dll. So that the application is working, for instance on another computer, you need to rewrite the two versions. Otherwise, a runtime error occurs. Something like Could not load file or assembly. The system can not find the file specified . So I would like to compile the application in such a way that in the end turned out, only one file – App2.exe. And in order to distribute this application it can be a single file. A file App1.dll was as if built into it.

Answer 1, Authority 100%

Possible solutions:

Merge assemblies one after compiling

Several assemblies can be glued to one after the compilation, using a tool ILMerge .

It has to NuGet.org :

Install-Package ilmerge

The format of the call:

 /lib:"C:\Windows\Microsoft.NET\Framework64\v4.0.30319 "
 / Lib: "C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ PublicAssemblies"
 / Targetplatform: v4
 /out:out.dll mydll1.dll mydll2.dll

before using the path cost to fix expired.

The same tools – il-repack , Mono.Merge

Enable DLL to EXE as Embedded Resource to replace the standard loading mechanism:

  1. Add bin \ someassembly.dll in exe project through the Add Existing Item / arrow on the Add / Add as Link, set the Build Type = Embedded Resource
  2. Add the project someassembly in the References. Align’s reference in the Copy Local = false – to avoid copying bin
  3. .

  4. Edit CurrentDomain_AssemblyResolve :

    using system;
    using System.Reflection;
    using System.Windows.Forms;
    namespace WindowsFormsApplication7
      Static Class Program
        Static Void Main ()
          AppDomain.CurrentDomain.AssemblyResolve + = CurrentDomain_AssemblyResolve;
          Application.enablevisualstyles ();
          Application.SetCompatibleTextRenderingDefault (False);
          Application.Run (New Form1 ());
        private static Assembly CurrentDomain_AssemblyResolve (object sender, ResolveEventArgs args)
          var assemblyName = new AssemblyName (args.Name) .Name;
          if (assemblyName == "someassembly")
            using (var stream = typeof (Program) .Assembly.GetManifestResourceStream (
              "WindowsFormsApplication7." + AssemblyName + ".dll"))
              byte [] assemblyData = new byte [stream.Length];
              stream.Read (assemblyData, 0, assemblyData.Length);
              return Assembly.Load (assemblyData);
            RETURN NULL;

This is the minimum working example. If it does not work – run under the debugger. Most likely you did not guess with the resource name, and getmanifestresourcestream returns NULL . Make sure the type of Atem is set to Embedded Resource (and not just in Resource). You can view the names of all available resources right in debugger, call

typeof (Program) .Assembly.getmanifestresourcenems ()

The project entirely on Hithabe, on the example of SevenzipSharp: https://github.com/paShapash/sevenzipSharp-Embedded

Enabling assemblies in the form of resources, automatic option

Install the costura.fody via Nuget and get one EXE at the output.

& gt; PM & GT; Install-Package Costura.fody


Answer 2, Authority 9%

for .NET 5.0 * There is a simpler, supported Microsoft solution: Single File Deployment .

You need to configure Publication . Go to Build → Publish, create a publication profile. Be sure to select a specific target runTime (for example, Windows 64 Bit), do not leave Portable.

You can also select Deployment Mode = Self-Contained, and all the .NET used to be added to the file, install the .NET Runtime on the user’s user computer (but the file size will grow significantly). You can also request a ReadyTorun compilation (that is, AOT).


* In fact, it should work with .NET Core 3.0, but I have not checked.

Answer 3, Authority 5%

I will correct myself, the small utility from Microsoft – Ilmerge will solve the problem. I will add that there is a good article on this topic

Answer 4, Authority 5%

There is also a crutch way: create a new project and add to it as existing all files with the code, then compile.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions