Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use vs2013.
I want to make a.exe file including a .dll file.
I tryed to use ILMerge but it doesn't work.

Could you find a answer for me?

C#
try
            {
                String strDir = @"C:\Users\Administrator\Desktop\df\document filter\bin\x64\Release\";

                String[] exeFiles = System.IO.Directory.GetFiles(@"C:\Users\Administrator\Desktop\df\document filter\bin\x64\Release\", "*.exe");
                String[] dllFiles = System.IO.Directory.GetFiles(@"C:\Users\Administrator\Desktop\df\document filter\bin\x64\Release\", "*.dll");

                ArrayList ar = new ArrayList();

                Boolean bAdded = false;

                //there might be more than 1 exe file, 
                //we go for the first one that isn't the vshost exe
                foreach (String strExe in exeFiles)
                {
                    if (!strExe.Contains("vshost"))
                    {
                        ar.Add(strExe);
                        bAdded = true;
                        break;
                    }
                }

                if (!bAdded)
                {
                    Console.WriteLine("Error: No exe could be found");
                    //I know multiple returns are bad…
                    return;
                }

                bAdded = false;

                foreach (String strDLL in dllFiles)
                {
                    ar.Add(strDLL);
                    bAdded = true;
                }

                //no point merging if nothing to merge with!
                if (!bAdded)
                {
                    Console.WriteLine("Error: No DLLs could be found");
                    //I know multiple returns are bad…
                    return;
                }


                //You will need to add a reference to ILMerge.exe from Microsoft
                //See http://research.microsoft.com/~mbarnett/ILMerge.aspx
                ILMerging.ILMerge myMerge = new ILMerging.ILMerge();

                String[] files = (String[])ar.ToArray(typeof(string));

                String strTargetDir = strDir + "\\Merged";

                try
                {
                    System.IO.Directory.CreateDirectory(strTargetDir);
                }
                catch
                {
                }

                //Here we get the first file name 
                //(which was the .exe file) and use that
                // as the output
                String strOutputFile = System.IO.Path.GetFileName(files[0]);

                myMerge.OutputFile = strTargetDir + "\\" + strOutputFile;
                myMerge.SetInputAssemblies(files);

                myMerge.Merge();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
Posted
Comments
Kornfeld Eliyahu Peter 8-Dec-14 5:28am    
Doesn't work? In what way?
Fredrik Bornander 8-Dec-14 7:09am    
Is there a particular reason for trying to do this from code rather than just use the IlMerge.exe command line?
Sergey Alexandrovich Kryukov 8-Dec-14 16:02pm    
Static linking? What do you mean? Aren't you are trying to apply your C++/C knowledge? :-)
Also, why would you need ILMerge at all? If you want to have a monolithic application assembly (why? why?!), you can just make a monolithic project...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900