在Visual C#2010中将DLL嵌入到.exe中 [英] Embedding DLL's into .exe in in Visual C# 2010

查看:807
本文介绍了在Visual C#2010中将DLL嵌入到.exe中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个使用iTextSharp.dll和WebCam_Capture.dll的C#程序。当我构建程序时,它会在debug文件夹中创建可执行文件,并且还会按预期将这两个dll复制到调试文件夹。我想将它们合并成一个可执行文件,但是我失败了。这两个库通常在解决方案资源管理器中可见。我也把它们添加为资源。可执行大小变得更大,等于三个文件的总和,但可执行文件仍然需要这些库在其目录中...我使用资源文件的构建操作属性播放,但没有更改。我也尝试了ILmerge,但它给了我一个错误。那么我该怎么办?

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 two dll's to the debug folder as expected. I want to merge them into a single executable, however I failed. These two libraries are visible in the references normally in the solution explorer. I also add them as resources. Executable size got bigger which equals the sum of three files, nevertheless the executable still requires these libraries in its directory... I played with "build action" property of the resource files but no change. I also tried ILmerge but it gave me an error. so what should I do?

更新:这是我从ILmerge获得的:

Update: This is what I get from ILmerge:

An exception occurred during merging:
Unresolved assembly reference not allowed: System.Core.
at System.Compiler.Ir2md.GetAssemblyRefIndex(AssemblyNode assembly)
   at System.Compiler.Ir2md.GetTypeRefIndex(TypeNode type)

它只是一个Windows应用程序,一个表单被填写和打印为pdf,通过网络摄像头拍摄照片(如果可用)。感谢所有!

It is just a windows application by the way, a form to be filled and printed as pdf with a photo taken via webcam if available. Thanks all!

推荐答案

您可以使用 ILMerge 将多个程序集合在一起。你已经说过你这样做了,你收到一个错误。虽然我不知道为什么,可以使用一个替代方法:如果这些库是开放源代码(并且它们的许可证与您的许可证兼容),您可以下载源代码,将其添加到您的项目并编译。这将导致单个程序集。

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.

ILMerge页面还列出了杰弗里·里奇特的博客是解决您的问题的另一个替代方案:

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


许多应用程序由依赖于许多DLL
文件的EXE文件组成。部署此应用程序时,所有文件必须已部署
。但是,您可以使用一种技术来部署
只需一个EXE文件。首先,确定您的
EXE文件所依赖的所有DLL文件不作为Microsoft .NET
框架本身的一部分。然后将这些DLL添加到Visual Studio项目中。
对于您添加的每个DLL文件,显示其属性并将其
Build Action更改为Embedded Resource。这将导致C#编译器
将DLL文件嵌入到EXE文件,您可以部署这个
EXE文件。

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.

在运行时,CLR将无法找到依赖的DLL
程序集,这是一个问题。要解决这个问题,当您的应用程序
初始化时,使用AppDomain的
ResolveAssembly事件注册一个回调方法。代码应该如下所示:

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); 
   }   
}; 

现在,线程首次调用引用
a依赖DLL中的类型的方法文件,将会引发AssemblyResolve事件,并且上面显示的
回调代码将找到所需的嵌入式DLL资源
,并通过调用Assembly的Load方法加载它,
需要一个Byte []作为参数。

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.

这篇关于在Visual C#2010中将DLL嵌入到.exe中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆