0

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:

https://www.youtube.com/watch?v=x-KK7bmo1AM

http://adamthetech.com/2011/06/embed-dll-files-within-an-exe-c-sharp-winforms/

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

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

Community
  • 1
  • 1
fishmong3r
  • 1,414
  • 4
  • 24
  • 51

2 Answers2

0

Try having a look at this in your application:

In the program.cs main method:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

Create the event:

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name.StartsWith("MyAssembly,"))
    {
        return Assembly.Load(Properties.Resources.MyAssembly_Dll);
    }
    return null;
}

I am using something similar for dynamically loading an assembly that requires other assemblies that have not yet been loaded and so I load them as required.

Comments welcome.

James
  • 2,812
  • 3
  • 22
  • 33
  • I added this event: `private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name.StartsWith("EPPlus")) { return Assembly.Load(Properties.Resources.EPPlus); } return null; }` It doesn't work. I can't use it like EPPlus.dll as if I add it to the resources like an existing file, it removes the extension. – fishmong3r Jul 24 '14 at 10:45
0

Download ILMerge from here then install ILMerge.

After that you should have the following file C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe or C:\Program Files\Microsoft\ILMerge\ILMerge.exe if you are on a 32bit system.

Then you execute ILMerge.exe with the following parameters /out:OutputExeFile.exe InputExeFile.exe dllfile.dll

After that ILMerge should have merged InputExeFile.exe and dllfile.dll and stored the result in OutputExeFile.exe

if you just run the ilmerge you will get the following help

Usage: ilmerge [/lib:directory]* [/log[:filename]] [/keyfile:filename [/delaysign]] [/internalize[:filename]] [/t[arget]:(library|exe|winexe)] [/closed] [/ndebug] [/ver:version] [/copyattrs [/allowMultiple] [/keepFirst]] [/xmldocs] [/attr:filename] [/targetplatform:[,] | /v1 | /v1.1 | /v2 | /v4] [/useFullPublicKeyForReferences] [/wildcards] [/zeroPeKind] [/allowDup:type]* [/union] [/align:n] /out:filename [...]

Peter
  • 37,042
  • 39
  • 142
  • 198
  • Where do you define the dll to be embedded? – fishmong3r Jul 24 '14 at 11:21
  • dllfile.dll is the dll that should be embeded, if you have mutiple you can use /wildcards and use *.dll or just list them one after the other.. – Peter Jul 24 '14 at 12:35
  • And I have to do this process every single time I build a new version of the exe, right? – fishmong3r Jul 24 '14 at 13:11
  • ILMerge error: `c:\Program Files (x86)\Microsoft\ILMerge>ilmerge.exe /out:RSMailer.exe "C:\Users\user\Google Drive\x\Projects\RSMailer\bin\Release\RSMailer.exe " "c:\Users\user\Google Drive\x\Projects\RSMailer\Resources\EPPlus.dll" 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) at System.Compiler.Ir2md.WriteTypeDefOrRefEncoded(BinaryWriter target, TypeNode type)` – fishmong3r Jul 24 '14 at 13:15
  • @fishmong3r did you try google that error? http://geekswithblogs.net/michelotti/archive/2010/06/02/ilmerge---unresolved-assembly-reference-not-allowed-system.core.aspx – Peter Jul 24 '14 at 18:49
  • OK, I added the config file and it's running. Actually I initiated the command 15 minutes ago. I suppose it's a bit longer then expected. The new exe has been created meanwhile with 0KB in size. – fishmong3r Jul 25 '14 at 07:24
  • I have no idea what file sizes you are working with but 15 minutes is extreme. for me it takes a few seconds – Peter Jul 25 '14 at 08:40
  • The exe and the dll together is no larger than 2.5 MB. – fishmong3r Jul 25 '14 at 08:42
  • I tried it again, but after 40 minutes I killed the process. – fishmong3r Jul 25 '14 at 08:42