SoFunction
Updated on 2025-03-06

Steps to package dll into exe in C# project

intention:

I want to compile the two dll library files (and) used in the project into the exe, and after compilation, only one exe program can be run independently without other files.

accomplish:

1. Add two dll library files to the project as resource files;

2. Add function code

using System;
using ;
using ;
using ;
using ;
 
 
namespace 
{
  /// <summary> Load the dynamic link library (dll) file in the resource  /// &lt;/summary&gt;
  static class LoadResourceDll
  {
    static Dictionary&lt;string, Assembly&gt; Dlls = new Dictionary&lt;string, Assembly&gt;();
    static Dictionary&lt;string, object&gt; Assemblies = new Dictionary&lt;string, object&gt;();
 
 
    static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
    {
      //Assembly      Assembly ass;
      //Get the full name of the assembly that failed to load      var assName = new AssemblyName().FullName;
      //Judge whether there is a loaded assembly of the same name in the Dlls collection      if ((assName, out ass) &amp;&amp; ass != null)
      {
        Dlls[assName] = null;//If there is, empty and return        return ass;
      }
      else
      {
        throw new DllNotFoundException(assName);// Otherwise, an exception that failed to load will be thrown      }
    }
 
 
    /// <summary> Register the dll in the resource    /// &lt;/summary&gt;
    public static void RegistDLL()
    {
      //Get the caller's assembly      var ass = new StackTrace(0).GetFrame(1).GetMethod().;
      //Judge whether the assembly has been processed      if (())
      {
        return;
      }
      //Assembly join the processed collection      (, null);
      //Binding assembly loading event failed (I tested it here, even if you bind it repeatedly, it doesn't matter)       += AssemblyResolve;
      //Get all resource file names      var res = ();
      foreach (var r in res)
      {
        //If it is dll, then load        if ((".dll", ))
        {
          try
          {
            if (("") 
              || ("CryptEnDe_d.dll"))
            {
              ExtractResourceToFile(r, () + @"/" + (('.') + 1));
            }
 
 
            var s = (r);
            var bts = new byte[];
            (bts, 0, (int));
            var da = (bts);
            //Judge whether it has been loaded            if (())
            {
              continue;
            }
            Dlls[] = da;
          }
          catch(Exception e)
          {
            //Forget it if the load fails...          }
        }
      }
    }
 
 
    private static void ExtractResourceToFile(string resourceName, string filename)
    {
      //if (!(filename))
      {
        using ( s = ().GetManifestResourceStream(resourceName))
        {
          using ( fs = new (filename, , , ))
          {
            byte[] b = new byte[];
            (b, 0, );
            (b, 0, );
          }
        }
      }
    }
 
 
  }
}

Where () function gets the path released by dll, specify the path according to your needs

public static string GetUpdateDllPath()
{
  string strPath = @"C:/LTShiyi/cache/updatedll";
  if (!(strPath))
  {
    (strPath);
  }
 
   return strPath;
}

3. Call the above interface function in the program entry Program class

static Program()
{
  ();
}

4. Just compile.

The above is the detailed content of the steps for packaging dlls into exe in the C# project. For more information about packaging c# dlls into exe, please pay attention to my other related articles!