Tuesday, June 20, 2017

embedding dlls into exe .net C#

https://stackoverflow.com/questions/6920920/embedding-dlls-into-exe-in-in-visual-c-sharp-2010

1: visual studio project include the dlls  Build Action as "Embedded Resource"

2: in program.cs  file  ,
    using System.Reflection;
    using System.IO;

3:  in main() function
      //Load embedded resources dll to CurrentDomain.
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                String dllName = new AssemblyName(args.Name).Name + ".dll";
                var assem = Assembly.GetExecutingAssembly();
                String resourceName = assem.GetManifestResourceNames().FirstOrDefault(rn => rn.EndsWith(dllName));
                if (resourceName == null) return null;
                using (var stream = assem.GetManifestResourceStream(resourceName))
                {
                    Byte[] assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }
            };


         


No comments:

Post a Comment