title: Embedding .dll into .exe
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4043522/embed-net-dll-in-c-sharp-exe">Embed .net dll in c# .exe</a>  </p>
  </blockquote>
  
  
  
  <p>I have been trying to embed a .dll (Specifically Ionic.Zip.dll) into my application, which then compiles a new .exe using CodeDom and requires the Ionic.Zip.dll. I want to be able to distribute my program without any additional .dll's. If there is a DLL in the working directory, the program and the compiled program runs fine. I am not using ILMerge because the compiled program requires the .dll and I can't force the users to get ILMerge. However, if there isn't a .dll I get this error. </p>
  
  <p>Original program won't compile the code with this error</p>
  
  <pre><code>error CS0006: Metadeta file 'Ionic.Zip.dll" could not be found.
  </code></pre>
  
  <p>Compiled program (Compiled while having the dll in the working directory of the original program)</p>
  
  <pre><code>Description:
  Stopped working
  
  Problem signature:
  Problem Event Name: CLR20r3
  Problem Signature 01:   scrub.exe
  Problem Signature 02:   0.0.0.0
  Problem Signature 03:   50b0f364
  Problem Signature 04:   Scrub
  Problem Signature 05:   0.0.0.0
  Problem Signature 06:   50b0f364
  Problem Signature 07:   1
  Problem Signature 08:   38
  Problem Signature 09:   System.IO.FileNotFoundException
  OS Version: 6.1.7601.2.1.0.256.48
  Locale ID:  1033
  </code></pre>
  
  <p>Debug Error</p>
  
  <pre><code>Could not load file or assembly 'Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c' or one of its dependencies. The system cannot find the file specified.
  </code></pre>
  
  <p>Within the CodeDom compiler I have used this code:</p>
  
  <pre><code>Params.ReferencedAssemblies.Add("Ionic.Zip.dll");
  </code></pre>
  
  <p>To fix this I've tried this and followed it as closely as I could. <a href="http://adamthetech.com/2011/06/embed-dll-files-within-an-exe-c-sharp-winforms/" rel="nofollow noreferrer">http://adamthetech.com/2011/06/embed-dll-files-within-an-exe-c-sharp-winforms/</a></p>
  
  <p>Here's my code within the original application:</p>
  
  <pre><code>        public MainWindow()
      {
          AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =&gt;
          {
              string resourceName = new AssemblyName(args.Name).Name + ".dll";
              string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element =&gt; element.EndsWith(resourceName));
  
              using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
              {
                  Byte[] assemblyData = new Byte[stream.Length];
                  stream.Read(assemblyData, 0, assemblyData.Length);
                  return Assembly.Load(assemblyData);
              }
          };
  
          InitializeComponent();
  
          inniZip();
       }
  
      private void inniZip()
      {
          Ionic.Zip.ZipOption test = new Ionic.Zip.ZipOption();
      }
  </code></pre>
  
  <p>I've put similar code in the compiled program source too.
  I've been stuck on this error for a while now and I just can't seem to figure it out. </p>
  
  <p>Thanks in advance,</p>