C# How To Embed And Reference An External Assembly (DLL) - Windows Forms
Why Stack Overflow recommends it
Stack Overflow users link it when someone needs to bundle a dependency DLL into one C# executable or fix a 'Could not load file or assembly' error after moving the exe.
Recommended by 4 people across 4 different months, 2013 to 2019.
What it is
A short 2013 tutorial by BetterCoder showing how to embed an external .NET DLL inside a Windows Forms executable so the app ships as a single file. It walks through adding the DLL as an embedded resource and hooking AppDomain.AssemblyResolve to load it at runtime.
What people said
Recommends reggaeguitar · 2013-05 · Stack Overflow · on “How to load Assembly at runtime and create class instance?”
Check out [this video] To modify his code to load multiple assemblies use static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { string assemblyName = args.Name.Split(',').First(); using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace." + assemblyName + ".dll")) { byte[] assemblyData = new byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } } In your main method put AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; Be sure to add your assemblies to your project and change the build action property to "Embedded Resource".
Recommends user613326 · 2013-10 · Stack Overflow · on “Get images from video in c#”
In visual studio you need to make a reference to the dll in your project As this dll is not part of the default .net files or system32 dlls this is how you make a reference [this video]
Recommends John Thompson · 2015-08 · Stack Overflow · on “Add reference to dll with not guaranteed path”
This video [this video] on YouTube has it. What you need to do is take the dll file and embed it in your program. From there, you will be able to load it with the code underneath. [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Program.CurrentDomain_AssemblyResolve); Application.Run(new Form1()); } private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EmbedAssembly.WhateverDll.dll")) { byte[] assemblyData = new byte[stream.Length]; stream.Rea
Recommends need2lease · 2015-09 · Stack Overflow · on “Using System.Management in my executable”
See the following YouTube Clip - worked like a charm (C# How To Embed And Reference An External Assembly (DLL) - Windows Forms) [this video]
Cites fishmong3r · 2014-07 · Stack Overflow · on “Can't embed EPPlus.dll into my exe”
I need to embed EPPlus.dll to my standalone exe app. I don't want it to be copied along the exe. Apparently icluding it as an assembly resource would solve my problem. I found many decriptions on how to do it. For e.g. I performed all the below: [this video] [LINK] Embedding DLL's into .exe in in Visual C# 2010 [LINK] Still I get Could not load file or assembly 'EPPlus.dll'... Can you please give me some idea as I have never done something like this before? P.S.: I use VS C# 2010 Express
Cites fishmong3r · 2014-07 · Stack Overflow · on “How convert stream excel file to datatable C#?”
@TimSchmelter Many thanks. I know it's a different thing but can you help me how to start on including the EPPlus.dll? I did this: [this video] but I still get `Could not load file or assembly`
Cites max · 2015-03 · Stack Overflow · on “how to merge dll's into one dll”
Using c# in visual studio 2012, I've created a class library FileTube.dll It uses 3 other dll's. I want to make a single dll to contain those thus I can publish my library via nuget. I have tried 3 approaches all failing: approach 1: In vistual studio, I set the "Embed Interop Assembly" to true, I get this error: Error 2 Cannot embed interop types from assembly 'DiffieHellman.dll' because it is missing the 'GuidAttribute' attribute DiffieHellman.dll approach 2: I used ILMergeGUI. It generated the code to use for ILMerge which fails with the error: Object reference not set to an instance of an object. Here is the command: "C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe" /ndebug /copyatt
Cites Litte Programmer · 2017-07 · Stack Overflow · on “issue with reference to .dll”
currently making a small program by using WinForms and 7zip unpacker. To make it easier added reference to "SevenZipSharp.dll" everything work good, untill i wanted to publish(debug) project. When i move the ".exe" file in Desktop got error : System.IO.FileNotFoundException: Could not load file or assembly 'X' or one of its dependencies". I embedded resource set in .dll file + Copy Local: set to true How i can make this programs to work without .dll in Current directory. EDIT I did it by use System.Reflections Solution here [this video] But i got now another problem with System.TypeInitializationException: 'The type initializer for 'SevenZip.SevenZipLibraryManager' threw an exception.. ---> Syste
Cites Fayaz · 2018-02 · Stack Overflow · on “How to add EPPlus Nuget package into my exe”
Thanks Jeremy - In VS 2017 Community version I went to Build > Configuration Manager > Selected "Release" from the configruation drop down > Close. Then rebuild. and then from the folder structure pulled the exe from the bin\relase folder and pasted on the desktop to try, and I get the same message as the one above (the one in the terminal window) Any other tips? I keep finding this video ( [this video] ) that might help but shouldnt it simpler when including a Nuget package with the release .exe program?
Cites Stein Åsmul · 2019-01 · Stack Overflow · on “How to bundle dependency .dll files with main product in WiX”
Single-file-deployment? : I am a little unsure if this is what you ask, but it seems you want to embed all dlls within a single .NET executable file? Ilmerge.exe : This is not my area of expertise , but there is this answer: Embedding DLLs in a compiled executable [LINK] that you can check out. The tool Ilmerge.exe [LINK] - how-to [LINK] (also see answer below). Mock-up: ILMerge.exe /target:winexe /out:All.exe Main.exe one.dll two.dll I have never used this. I would be very surprised if it worked for all cases. Obviously. Worth a try? Test merged binary using virustotal.com [LINK] (release mode binary) to determine if you trigger any malware warnings. Resource File Embedding : You can also e