编译/合并DLL到独立的exe与wpf

本文关键字:exe wpf 独立 合并 DLL 编译 | 更新日期: 2023-09-27 18:01:32

Background:用wpf将dll合并成一个.exe

我该如何将。dll引用合并到。exe文件中,我阅读了上面的帖子,得到了它背后的原理,但我无法弄清楚如何做到这一点?(对不起,我是新手)参考文件是HtmlagilityPack.dll

目前我的App.xaml.cs包含:

public partial class App : Application
    {
       public App(){
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
            // proceed starting app...
        }
        static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
        {
            //We dont' care about System Assembies and so on...
            if (!args.Name.ToLower().StartsWith("Html")) return null;
            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;
        }
    }

我还应该在哪里做更改?,我一直在尝试过去一个小时与http://blog.mahop.net/post/Merge-WPF-Assemblies.aspx的一个例子,但无法弄清楚如何做到这一点与htmllagilitypack。

编译/合并DLL到独立的exe与wpf

好的,最后不得不使用SmartAssembly程序。但仍在寻找一个解决方案来做的代码。

您的代码看起来有点偏离,它应该看起来更像这样:

public class App : Application
{
    [STAThreadAttribute()]
    public static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
        // etc...
    }
    // etc...

你还需要改变项目属性页中的"启动对象"设置来使用App类(即上面的代码)-然后你应该看到这个类的Main方法是当你开始调试时执行的第一个代码。