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

前端 未结 8 2294
臣服心动
臣服心动 2020-12-14 10:31

I\'m working on a C# program that uses iTextSharp.dll and WebCam_Capture.dll. When I build the program, it creates executable in the debug folder and it also copies these tw

8条回答
  •  自闭症患者
    2020-12-14 11:00

    You can use ILMerge to merge multiple assemblies together. You've already said you did this, and you've received an error. Though I don't know why, you can use an alternative: if the libraries are open source (and their licenses are compatible with yours), you can download the source code, add it to your project and compile. This will result in a single assembly.

    The ILMerge page also lists Jeffrey Richter's blog as yet another alternative to solve your issue:

    Many applications consist of an EXE file that depends on many DLL files. When deploying this application, all the files must be deployed. However, there is a technique that you can use to deploy just a single EXE file. First, identify all the DLL files that your EXE file depends on that do not ship as part of the Microsoft .NET Framework itself. Then add these DLLs to your Visual Studio project. For each DLL file you add, display its properties and change its “Build Action” to “Embedded Resource.” This causes the C# compiler to embed the DLL file(s) into your EXE file, and you can deploy this one EXE file.

    At runtime, the CLR won’t be able to find the dependent DLL assemblies, which is a problem. To fix this, when your application initializes, register a callback method with the AppDomain’s ResolveAssembly event. The code should look something like this:

    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { 
       String resourceName = "AssemblyLoadingAndReflection." + 
           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); 
       }   
    }; 
    

    Now, the first time a thread calls a method that references a type in a dependent DLL file, the AssemblyResolve event will be raised and the callback code shown above will find the embedded DLL resource desired and load it by calling an overload of Assembly’s Load method that takes a Byte[] as an argument.

提交回复
热议问题