C# 将 DLL 合并到 EXE 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10137937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge DLL into EXE?
提问by Momro
I have two DLL files which I'd like to include in my EXE file to make it easier to distribute it. I've read a bit here and there how to do this, even found a good thread here, and here, but it's far too complicated for me and I need real basic instructions on how to do this.
我有两个 DLL 文件,我想将它们包含在我的 EXE 文件中,以便更轻松地分发它。我在这里和那里阅读了一些如何做到这一点,甚至在这里和这里找到了一个很好的线程,但这对我来说太复杂了,我需要关于如何做到这一点的真正基本说明。
I'm using Microsoft Visual C# Express 2010, and please excuse my "low standard" question, but I feel like I'm one or two level below everyone else's expercise :-/ If someone could point out how to merge these DDL files into my EXE in a step-by-step guide, this would be reallyawesome!
我正在使用 Microsoft Visual C# Express 2010,请原谅我的“低标准”问题,但我觉得我比其他人的经验低一两个级别:-/ 如果有人可以指出如何将这些 DDL 文件合并到我的 EXE 分步指南,这真的很棒!
采纳答案by Yuki Kutsuya
For .NET Framework 4.5
对于 .NET Framework 4.5
ILMerge.exe /target:winexe /targetplatform:"v4,C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" /out:finish.exe insert1.exe insert2.dll
- Open CMD and cd to your directory. Let's say:
cd C:\test - Insert the above code.
/out:finish.exereplacefinish.exewith any filename you want.- Behind the
/out:finish.exeyou have to give the files you want to be combined.
- 打开 CMD 并 cd 到您的目录。让我们说:
cd C:\test - 插入上面的代码。
/out:finish.exe替换finish.exe为您想要的任何文件名。- 在后面
/out:finish.exe你必须给出你想要组合的文件。
回答by TheVoidSeeker
回答by Alex
I answered a similar question for VB.NET. It shouldn't however be too hard to convert. You embedd the DLL'sinto your Ressourcefolder and on the first usage, the
AppDomain.CurrentDomain.AssemblyResolveevent gets fired.
我回答了一个类似的问题VB.NET。然而,转换应该不会太难。您将DLL's嵌入到您的Ressource文件夹中,并在第一次使用AppDomain.CurrentDomain.AssemblyResolve时触发该
事件。
If you want to reference it during development, just add a normal DLLreference to your project.
如果你想在开发过程中引用它,只需DLL在你的项目中添加一个普通的引用即可。
回答by Henrik Karlsson
Install ILMergeas the other threads tell you to
Then go to the installation folder, by default
C:\Program Files (x86)\Microsoft\ILMergeDrag your Dll's and Exes to that folder
Shift-Rightclick in that folder and choose open command prompt
Write
ilmerge myExe.exe Dll1.dll /out:merged.exeNote that you should write your exe first.
按照其他线程告诉您的方式安装 ILMerge
然后进入安装文件夹,默认
C:\Program Files (x86)\Microsoft\ILMerge将您的 Dll 和 Exes 拖到该文件夹中
Shift-Rightclick 在该文件夹中并选择打开命令提示符
写
ilmerge myExe.exe Dll1.dll /out:merged.exe请注意,您应该先编写您的exe。
There you got your merged exe. This might not be the best way if your going to do this multiple times, but the simplest one for a one time use, I would recommend putting Ilmerge to your path.
在那里你得到了合并的 exe。如果您要多次执行此操作,这可能不是最好的方法,但是一次使用最简单的方法,我建议将 Ilmerge 放在您的路径中。
回答by Destructor
Reference the DLL′s to your Resources and and use the AssemblyResolve-Event to return the Resource-DLL.
将 DLL 引用到您的资源,并使用 AssemblyResolve-Event 返回资源 DLL。
public partial class App : Application
{
public App()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
Assembly thisAssembly = Assembly.GetExecutingAssembly();
//Get the Name of the AssemblyFile
var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";
//Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
if (resources.Count() > 0)
{
var resourceName = resources.First();
using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
{
if (stream == null) return null;
var block = new byte[stream.Length];
stream.Read(block, 0, block.Length);
return Assembly.Load(block);
}
}
return null;
};
}
}
回答by Zain Ali
Also you can use ilmergertool at codeplex with GUI interface.
您也可以在带有 GUI 界面的 codeplex 中使用 ilmergertool。
回答by Kieran Hill
Download ilmergeand ilmergre gui. makes joining the files so easy ive used these and works great
下载ilmerge和ilmergre gui。使加入文件变得如此容易我使用过这些并且效果很好
回答by Paulos02
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
/* PUT THIS LINE IN YOUR CLASS PROGRAM MAIN() */
AppDomain.CurrentDomain.AssemblyResolve += (sender, arg) => { if (arg.Name.StartsWith("YOURDLL")) return Assembly.Load(Properties.Resources.YOURDLL); return null; };
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
First add the DLL′s to your project-Resources. Add a folder "Resources"
首先将 DLL 添加到您的项目资源中。添加文件夹“资源”
回答by Igor Popov
Use Costura.Fody.
使用Costura.Fody。
You just have to install the nugetand then do a build. The final executable will be standalone.
您只需要安装nuget,然后进行构建。最终的可执行文件将是独立的。
回答by Yuan
The command should be the following script:
命令应该是以下脚本:
ilmerge myExe.exe Dll1.dll /target:winexe /targetplatform:"v4,c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\" /out:merged.exe /out:merged.exe

