[英]Merge DLL into EXE?
對於 .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
cd C:\\test
/out:finish.exe
用您想要的任何文件名替換finish.exe
。/out:finish.exe
后面你必須給出你想要組合的文件。使用Costura.Fody 。
您只需要安裝nuget ,然后進行構建。 最終的可執行文件將是獨立的。
下載ilmerge和ilmergre gui 。 使加入文件變得如此容易我使用過這些並且效果很好
將 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;
};
}
}
按照其他線程告訴您的方式安裝 ILMerge
然后進入安裝文件夾,默認為C:\\Program Files (x86)\\Microsoft\\ILMerge
將您的 Dll 和 Exes 拖到該文件夾中
Shift-Rightclick 在該文件夾中並選擇打開命令提示符
寫
ilmerge myExe.exe Dll1.dll /out:merged.exe
請注意,您應該先編寫您的exe。
在那里你得到了合並的 exe。 如果您要多次執行此操作,這可能不是最好的方法,但這是一次使用最簡單的方法,我建議將 Ilmerge 放在您的路徑中。
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());
}
}
首先將 DLL 添加到您的項目資源中。 添加文件夾“資源”
2019年更新(僅供參考):
從.NET Core 3.0 開始,此功能開箱即用。 要利用單文件可執行文件發布,只需在項目配置文件中添加以下行:
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
現在, dotnet publish
應該在不使用任何外部工具的情況下生成單個 .exe 文件。
有關此功能的更多文檔,請訪問https://github.com/dotnet/designs/blob/master/accepted/single-file/design.md 。
您也可以在帶有 GUI 界面的 codeplex 中使用ilmergertool 。
這是官方文檔。 這也會在步驟 2 中自動下載。
下面是一個非常簡單的方法,我已經使用.NET framework 4.6.1
成功構建了我的應用程序
通過 gui 或命令行安裝 ILMerge nuget 包:
Install-Package ilmerge
確認您已下載它。 現在安裝(不確定此命令,但只需轉到您的 nuget 包): 注意:如果您有多個解決方案,您可能只需要為其中一個解決方案安裝它
導航到您的解決方案文件夾,在包文件夾中,您應該會看到帶有可執行文件的“ILMerge”:
\\FindMyiPhone-master\\FindMyiPhone-master\\packages\\ILMerge.2.14.1208\\tools
現在這里是可執行文件,您可以將其復制到您的\\bin\\Debug
(或任何構建您的應用程序的位置),然后在命令行/powershell 中執行如下操作:
ILMerge.exe myExecutable.exe myDll1.dll myDll2.dll myDlln.dll myNEWExecutable.exe
您現在將擁有一個包含所有庫的新可執行文件!
我為VB.NET
回答了一個類似的問題。 然而,轉換應該不會太難。 您將DLL's
嵌入到您的Ressource
文件夾中,並且在第一次使用時, AppDomain.CurrentDomain.AssemblyResolve
事件被觸發。
如果您想在開發過程中引用它,只需在您的項目中添加一個普通的DLL
引用即可。
注意:如果您嘗試加載非 ILOnly 程序集,則
Assembly.Load(block)
將不起作用,並且會拋出異常: 更多詳細信息
我通過創建一個臨時文件並使用
Assembly.LoadFile(dllFile)
我找到了以下解決方案:-
對於多個 Dll:-
C:\\Program Files (x86)\\Microsoft\\ILMerge>ILMerge.exe /target:winexe /targetplatform:"v4,C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319" /out:NewExeName.exe SourceExeName.exe DllName1.dll DllName2.dll DllName3.dll
命令應該是以下腳本:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.