How to bundle MahApps.Metro into single exe

拜拜、爱过 提交于 2019-11-29 07:01:49

here is my way to put the dlls in a single exe

1) create a folder named DllsAsResource and put the MahApps.Metro.dll and System.Windows.Interactivity.dll with Add as Link to it

2) change the Build Action for the dlls to Embedded Resource

3) change Copy Local to false for the normal referenced dlls

4) create Program.cs

using System;
using System.Reflection;

namespace MahApps.Metro.Simple.Demo
{
  public class Program
  {
    [STAThread]
    public static void Main()
    {
      AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
        var resourceName = Assembly.GetExecutingAssembly().GetName().Name + ".DllsAsResource." + new AssemblyName(args.Name).Name + ".dll";
        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
          if (stream != null) {
            var assemblyData = new Byte[stream.Length];
            stream.Read(assemblyData, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
          }
        }
        return null;
      };

      App.Main();
    }
  }
}

5) set the Startup object to the Program.cs in your project properties

6) now you have a single exe without publishing the hole dlls

you can look at this demo for all the tips

hope that helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!