TarunG
TarunG

Reputation: 610

compiling/merging dll into standalone exe with wpf

Background : Merging dlls into a single .exe with wpf

How shall i merge a .dll reference into the .exe file, i read the above post, got principle behind it, but i am not able to figure out how to do it?( i am newbie, sorry) The reference file is HtmlagilityPack.dll

Currently my App.xaml.cs contains :

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;
        }
    }

Where else am i supposed to make changes?, i have being trying past an hour with an example of http://blog.mahop.net/post/Merge-WPF-Assemblies.aspx But not able to figure out how to do it with HtmlAgilityPack.

Upvotes: 1

Views: 1469

Answers (2)

Justin
Justin

Reputation: 86789

Your code looks slightly off, it should look more like this:

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

    // etc...

You then also need to change the "Startup object" setting in the properties page of your project to use the App class (i.e. the above code) - you should then see the Main method of this class being the first code executed when you start debugging.

Upvotes: 0

TarunG
TarunG

Reputation: 610

Okay, finally had to use the SmartAssembly program. But still looking for a solution to do it by code.

Upvotes: 1

Related Questions