簡體   English   中英

將 DLL 合並到 EXE 中?

[英]Merge DLL into EXE?

我有兩個 DLL 文件,我想將它們包含在我的 EXE 文件中,以便更輕松地分發它。 我在這里和那里閱讀了一些如何做到這一點,甚至在這里這里找到了一個很好的線程,但這對我來說太復雜了,我需要關於如何做到這一點的真正基本說明。

我正在使用 Microsoft Visual C# Express 2010,請原諒我的“低標准”問題,但我覺得我比其他人的經驗低一兩個級別:-/ 如果有人可以指出如何將這些 DDL 文件合並到我的 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

合並

  1. 打開 CMD 並 cd 到您的目錄。 比方說: cd C:\\test
  2. 插入上面的代碼。
  3. /out:finish.exe用您想要的任何文件名替換finish.exe
  4. /out:finish.exe后面你必須給出你想要組合的文件。

使用Costura.Fody

您只需要安裝nuget ,然后進行構建 最終的可執行文件將是獨立的

下載ilmergeilmergre 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 /target:winexe /out:c:\output.exe c:\input.exe C:\input.dll
  1. 按照其他線程告訴您的方式安裝 ILMerge

  2. 然后進入安裝文件夾,默認為C:\\Program Files (x86)\\Microsoft\\ILMerge

  3. 將您的 Dll 和 Exes 拖到該文件夾​​中

  4. Shift-Rightclick 在該文件夾中並選擇打開命令提示符

  5. 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成功構建了我的應用程序

  1. 通過 gui 或命令行安裝 ILMerge nuget 包:

     Install-Package ilmerge
  2. 確認您已下載它。 現在安裝(不確定此命令,但只需轉到您的 nuget 包): 在此處輸入圖片說明 注意:如果您有多個解決方案,您可能只需要為其中一個解決方案安裝它

  3. 導航到您的解決方案文件夾,在包文件夾中,您應該會看到帶有可執行文件的“ILMerge”:

     \\FindMyiPhone-master\\FindMyiPhone-master\\packages\\ILMerge.2.14.1208\\tools

    在此處輸入圖片說明

  4. 現在這里是可執行文件,您可以將其復制到您的\\bin\\Debug (或任何構建您的應用程序的位置),然后在命令行/powershell 中執行如下操作:

     ILMerge.exe myExecutable.exe myDll1.dll myDll2.dll myDlln.dll myNEWExecutable.exe

您現在將擁有一個包含所有庫的新可執行文件!

我為VB.NET回答了一個類似的問題。 然而,轉換應該不會太難。 您將DLL's嵌入到您的Ressource文件夾中,並且在第一次使用時, AppDomain.CurrentDomain.AssemblyResolve事件被觸發。

如果您想在開發過程中引用它,只需在您的項目中添加一個普通的DLL引用即可。

將 DLL 嵌入到項目中

注意:如果您嘗試加載非 ILOnly 程序集,則

Assembly.Load(block)

將不起作用,並且會拋出異常: 更多詳細信息

我通過創建一個臨時文件並使用

Assembly.LoadFile(dllFile)

我找到了以下解決方案:-

  1. 下載 ILMerge.msi 並將其安裝在您的機器上。
  2. 打開命令提示符
  3. 鍵入 cd C:\\Program Files (x86)\\Microsoft\\ILMerge Preess Enter
  4. 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文件名.dll

對於多個 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM