用wpf编译/合并dll到独立的exe

时间:2011-07-06 10:05:39

标签: c# .net wpf dll

背景:Merging dlls into a single .exe with wpf

如何将.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但是无法弄清楚如何使用HtmlAgilityPack。

2 个答案:

答案 0 :(得分:1)

好的,最后不得不使用SmartAssembly程序。 但仍在寻找通过代码来实现的解决方案。

答案 1 :(得分:0)

你的代码看起来有些偏差,看起来应该更像这样:

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

    // etc...

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