将DLL合并到EXE中?

Mom*_*mro 52 c# dll merge ilmerge

我有两个DLL文件,我想包含在我的EXE文件中,以便更容易分发它.我在这里和那里读了一些如何做到这一点,甚至在这里找到了一个好的线程,在这里,但它对我来说太复杂了,我需要关于如何做到这一点的真正基本指令.

我正在使用Microsoft Visual C#Express 2010,请原谅我的"低标准"问题,但我觉得我比其他人的经验低一两级: - /如果有人可以指出如何将这些DDL文件合并到我的EXE在一步一步的指导中,这真的很棒!

Yuk*_*uya 86

对于.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
Run Code Online (Sandbox Code Playgroud)

ILMerge

  1. 打开CMD并cd到您的目录.让我们说:cd C:\test
  2. 插入上面的代码.
  3. /out:finish.exe替换finish.exe为您想要的任何文件名.
  4. /out:finish.exe你必须提供你想要组合的文件后面.

  • 如上所述,我只是按照你的指示行事.仍然没有合并.请帮我.在命令提示符中:`ILMerge.exe/target:winexe/targetplatform:"v4,C:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"/out:finish.exe myExe.exe mfc100u.dll mfc110u.dll msvcp110.dll`错误:`合并期间发生异常:ILMerge.Merge:无法从位置'C:\ test\my folder\mfc 100u.dll'加载程序集.跳过并处理其余的论点.任何想法是什么? (6认同)
  • 有时你必须1)使用`Program Files(x86)`2)输入ILMerge的完整路径.对控制台应用程序使用`/ target:exe`. (3认同)

小智 25

下载ilmergeilmergre gui.使加入文件如此简单我已经使用过这些并且效果很好


Igo*_*pov 22

使用Costura.Fody.

你只需要安装nuget然后进行构建.最终的可执行文件将是独立的.

  • 这是伏都教.令人难以置信的轻松.`Install-Package Costura.Fody`和Bam!你的构建将产生一个大的脂肪exe. (4认同)

Des*_*tor 16

将DLL引用到您的Resources,并使用AssemblyResolve-Event返回Resource-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;
        };
    }
}
Run Code Online (Sandbox Code Playgroud)


The*_*ker 10

下载

ILMerge

呼叫

ilmerge /target:winexe /out:c:\output.exe c:\input.exe C:\input.dll
Run Code Online (Sandbox Code Playgroud)


Hen*_*son 8

  1. 按其他线程告诉您安装ILMerge

  2. 然后转到安装文件夹,默认情况下 C:\Program Files (x86)\Microsoft\ILMerge

  3. 将Dll和Exes拖到该文件夹​​中

  4. 按住Shift键并右键单击该文件夹,然后选择打开命令提示符

  5. ilmerge myExe.exe Dll1.dll /out:merged.exe
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,您应该首先编写您的exe.

你有合并的exe.如果你多次这样做,这可能不是最好的方法,但最简单的一次使用,我建议把Ilmerge放到你的路上.


小智 6

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

首先将DLL添加到项目资源中.添加文件夹"资源"


alx*_*ull 6

2019年更新(仅供参考):

.NET Core 3.0开始,开箱即用地支持此功能。要利用单文件可执行文件发布,只需将以下行添加到项目配置文件中:

<PropertyGroup>
  <PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

现在,dotnet publish应该在不使用任何外部工具的情况下生成单个 .exe 文件。

有关此功能的更多文档,请访问https://github.com/dotnet/designs/blob/master/accepted/single-file/design.md