Embedding DLL's into .exe in in Visual C# 2010

Viewed 37275

I'm working on a C# program that uses iTextSharp.dll and WebCam_Capture.dll. When I build the program, it creates executable in the debug folder and it also copies these two dll's to the debug folder as expected. I want to merge them into a single executable, however I failed. These two libraries are visible in the references normally in the solution explorer. I also add them as resources. Executable size got bigger which equals the sum of three files, nevertheless the executable still requires these libraries in its directory... I played with "build action" property of the resource files but no change. I also tried ILmerge but it gave me an error. so what should I do?

Update: This is what I get from ILmerge:

An exception occurred during merging:
Unresolved assembly reference not allowed: System.Core.
at System.Compiler.Ir2md.GetAssemblyRefIndex(AssemblyNode assembly)
   at System.Compiler.Ir2md.GetTypeRefIndex(TypeNode type)

It is just a windows application by the way, a form to be filled and printed as pdf with a photo taken via webcam if available. Thanks all!

8 Answers

I know that topic is old but i'll write it for future persons that will want to use it.

i base on code by userSteve.

i would suggest to change this.

String thisExe = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

into this

String thisExe = System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.Namespace;

that way it would work even if namespace is different than assembly name

also if you want to use DLL from directory you can use it like that (directory Resources as Example)

String resourceName = thisExe + ".Resources." + embeddedAssembly.Name + ".dll";

if you still can't find where place this code in C# Form application paste it inside file "Program.cs" above line:

Application.Run(new Form_1());

and below lines:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Related