This question has already been answered:

In one folder there is an ekzeshnik and zedgraph.dll, but if I delete from the zedgraph.dll folder then ekheshnik does not start. How to solve this problem. It is necessary that the program runs without this library. Can I include it in the eksheshnik?

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

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 .

  • 7
    The solution is simple: do not delete the DLL, as it is necessary for the application to work. - Vladimir Martyanov
  • one
    What problem do you really want to solve? - Nofate
  • one
    The DLL is not intended for static linking, because it is a dynamic library. - Vladimir Martyanov
  • one
    @ Vladimir Martiyanov is not a suspicion, but a statement. It's like trying to run a .NET application without .NET. Just the native .NET DLL is not in the project folder but in the GAC and on Win Vista + in some version installed by default, so the illusion that they are not there appears. But this is just an illusion - rdorn
  • four
    Well, bring this to your user shortcut to launch the application on the desktop. Then he won’t be interested in the place where the executable file is located and what libraries are next to it. - aleks.andr

2 answers 2

How do I do it:

  1. I add the assembly as a regular file to the project.

  2. I specify the assembly (PKM on it, properties) Build Action - Embedded Resource, Do not copy.

  3. At the start of the application, I subscribe to the rezolv assembly event.

    AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly.ResolveInternalAssembly; internal static Assembly ResolveInternalAssembly(object sender, ResolveEventArgs args) { var resource = args.Name; var resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames(); if (!resourceNames.Any()) return null; if (resource.Contains(',')) resource = resource.Substring(0, resource.IndexOf(',')); var assemblyFile = string.Format(".{0}.dll", resource); var resourceName = resourceNames.SingleOrDefault(s => s.EndsWith(assemblyFile)); if (string.IsNullOrWhiteSpace(resourceName)) { assemblyFile = assemblyFile.Remove(0, 1); resourceName = resourceNames.SingleOrDefault(s => s.EndsWith(assemblyFile)); } if (string.IsNullOrWhiteSpace(resourceName)) return null; using (var stream = assembly.GetManifestResourceStream(resourceName)) { var block = new byte[stream.Length]; stream.Read(block, 0, block.Length); return Assembly.Load(block); } } 
  • An interesting option, but what are the pros and cons? not to argue, just interesting. - rdorn
  • @rdorn is all inside, a simple portable program. - Monk
  • But at the time of launch does not affect? - rdorn
  • When some nhibernate is running for 3 seconds, access to the libs is not noticeable, for example. - Monk
  • OK, thanks, another admission to the piggy bank. - rdorn 5:53 pm

There are utilities that collect an ex-shnik with several libraries into one - this is ILMerge, libz

  • This will not work if the library is native. And in general a good option. - VladD
  • @VladD yes, of course. on garlic, I prefer the solution with EmbeddedResource - kvvk
  • @VladD, and how is the search for native libraries? I suspect my decision does not work for them either, because they do not cause an event, apparently. - Monk
  • @kvvk: The solution to AssemblyResolve is the same problem, in general. Loading native libraries is not so flexible. (Although, perhaps, there are WinAPI'shnye hooks.) - VladD
  • @Monk: Yeah, this is only for .NET assemblies. - VladD