This question has already been answered:

I wrote a small project in VS 2013 using a third-party library (DLL). Now, next to the executable, you have to constantly keep this library. Is it possible to embed this library in the project or at least the necessary part of the code, so that this dll would not have to be carried everywhere?

Reported as a duplicate by PashaPash member c # 28 Dec '16 at 8:26

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • @Vitokhv this question - a complete duplicate question by reference. This is exactly the same question with the exact same solutions (resources + AssemblyResolve or ilmerge). If this is not the case, show what is different in the question. Regarding the style of communication - please re-read www.stackoverflow.com/help/be-nice . - PashaPash
  • @Vitokhv your question, by the way, is also a duplicate. as you indicated in the comments, Ionic.Zip.Reduced.dll is stitched in exe in the same way - by stitching into resources or by running ilmerge. There is no difference whether the dll is connected as a neighboring project or connected via nuget. - PashaPash
  • The explanation is simple, if you indicated my duplicate on this topic, all questions would disappear from me. But for some reason you decided that this topic is more important than those that are really important. The answer in this topic is more suitable for me than in another. - Vitokhv
  • @Vitokhv is not “I decided” - these are the rules of the site - for one specific problem - one question with answers from several participants. The answer that came up to you more - almost a complete copy of the solution number 2 from ru.stackoverflow.com/a/471236/177221 - the same mechanism, the same handler, the code has minor details. Yes, the details are important - and the mechanism of duplicates just exists in order to collect answers from different authors in one question. And not to force participants to look for a "suitable answer" for the top ten identical topics - PashaPash
  • one
    @Vitokhv that topic is selected as a target for a duplicate because it was created half a year earlier than this one. Those. when topikaster came here with a question - there was already a ready answer. It would be the opposite - would close the opposite. - PashaPash

3 answers 3

Yes you can. A dependent library can be embedded into resources and loaded from there manually. It is assumed that your library is already in the References project, the project is being assembled, the program is launched successfully

First, add the library as a resource to the project:

  • The Project menu -> Add Existing Item ... - choose your assembly (dll, in the open dialog, select the file type Executable Files). The library appears in the project file list.
  • Call the context menu on the added file, select Properties. Then install Build Actions -> Embedded Resource in the opened window.

Add the following class to your project:

public static class Resolver { private static volatile bool _loaded; public static void RegisterDependencyResolver() { if (!_loaded) { AppDomain.CurrentDomain.AssemblyResolve += OnResolve; _loaded = true; } } private static Assembly OnResolve(object sender, ResolveEventArgs args) { Assembly execAssembly = Assembly.GetExecutingAssembly(); string resourceName = String.Format("{0}.{1}.dll", execAssembly.GetName().Name, new AssemblyName(args.Name).Name); using (var stream = execAssembly.GetManifestResourceStream(resourceName)) { int read = 0, toRead = (int)stream.Length; byte[] data = new byte[toRead]; do { int n = stream.Read(data, read, data.Length - read); toRead -= n; read += n; } while (toRead > 0); return Assembly.Load(data); } } } 

This class will load the dependent assembly from the resource as soon as the main program accesses it using the AppDomain.AssemblyResolve event handler.

In the class where the program entry point is located, add a static constructor and call the RegisterDependencyResolver method. For example, suppose you have a console application:

 class Program { static Program() { Resolver.RegisterDependencyResolver(); } static void Main(string[] args) { // ... } } 

Take care of unloading the assembly is not necessary, because it is still impossible (you can only upload the entire domain).

An example is peeped in the book by J. Richter "CLR via C #".

  • Gorgeous! Thank you very much, everything works! - Darrio
  • one
    "stream.Read (data, 0, data.Length);" - Does someone guarantee that all content will be given away right away? - Qwertiy
  • @Qwertiy corrected. - kmv
  • @kmv thanks for this detailed answer, everything is clear and understandable. - Vitokhv

Another option is to use ILMerge . But you will have to write a script for “gluing” manually, there is no integration with the studio out of the box.

Also this option will not work for any library; If the library actively uses reflection, you may need to write your AssemblyResolve handler:

 var assembly = typeof(Program).Assembly; AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { if (args.Name == "имя внедренной сборки") return assembly; return null; }; 

The main advantage of the variant with ILMerge when working with several projects is that you do not need to put the same loader code in each project; since the AssemblyResolve handler is needed only for reflection - you can put it in the shared library.

    Use the utility ILMerge, or rather ILMergeGUI Excellent utility, packs all DLLs into EXE