The dll file is embedded exe program

Recently developed an installer needs to be embedded in the exe dll files to ensure that translation of the program has only one .exe file

  1. First, define the functions to obtain the dynamic dll file

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
     string resourceName = "MyProject." + new AssemblyName(args.Name).Name + ".dll";
     using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
     {
          byte[] assemblyData = new byte[stream.Length];
          stream.Read(assemblyData, 0, assemblyData.Length);
          return Assembly.Load(assemblyData);
     }
}

  Method 2. Add a line of code in the Main function, call the above when the program parses the assembly failure event triggered dynamically loaded assembly

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

  3. The need to embed dll file added to the project Build Action property in the Properties panel to modify the file or can not find the dll file as an embedded resource in the exe 

  

  4. In the application of the project still need to be added to the application dll file, otherwise the compiler can not pass, but copying the properties panel referenced dll file into the local property modifications to False, 

  

 

Guess you like

Origin www.cnblogs.com/applebox/p/11611551.html