none
Embed DLL & DOCX In Winform RRS feed

  • Question

  • I follow the Adam Tech article to embed dll in my winforms all the time, but now I have situation where I want to embed a dll and a docx.  I tried to just simply copy/paste the code and change variable names, but I am getting unreachable code detected message and need to ensure that the dll and docx are embedded.

    What would be the proper way to write this code?

    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {
        string resourceName = new AssemblyName(args.Name).Name + ".dll";
        string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));
     
        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
        {
            Byte[] assemblyData = new Byte[stream.Length];
            stream.Read(assemblyData, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
        }
    	string rn1 = new AssemblyName(args.Name).Name + ".docx";
        string rs1 = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(rn1));
     
        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(rs1))
        {
            Byte[] assemblyData = new Byte[stream.Length];
            stream.Read(assemblyData, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
        }
    };

    Friday, March 16, 2018 12:08 AM

Answers

  • Split them up rather than get complex is my recommendation.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by IndigoMontoya Friday, March 16, 2018 1:43 AM
    Friday, March 16, 2018 12:40 AM
    Moderator

All replies

  • I would suppose the issue is that there is a return in the first using and second using statement which means the first our ranks the second and will never be reached.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 16, 2018 12:33 AM
    Moderator
  • I would suppose the issue is that there is a return in the first using and second using statement which means the first our ranks the second and will never be reached.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Ah - such a good catch on that.  How could I embed two different extensions?
    Friday, March 16, 2018 12:36 AM
  • Split them up rather than get complex is my recommendation.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by IndigoMontoya Friday, March 16, 2018 1:43 AM
    Friday, March 16, 2018 12:40 AM
    Moderator