Joseph
Joseph

Reputation: 963

Getting Basic ILMerge Case Working

I am trying to get a basic VS 2012 project working with ILMerge, so I can eventually move up to more complicated examples (and eventually be able to use it with my real project).

Currently I have a working project, TestMain, and a reference library, TestDLL in the working project.

My working directory (..\bin\Release) is as follows:

My terminal output is as follows:

C:...\bin\Release>ilmerge /target:winexe /out:ContainedProgram.exe "TestMain.exe" "TestDLL.dll"

An exception occurred during merging: ILMerge.Merge: Could not load assembly from the location 'C:...\bin\Release\TestMain.exe'. Skipping and processing rest of arguments. at ILMerging.ILMerge.Merge()
at ILMerging.ILMerge.Main(String[] args)

Pretty simple, right? What am I missing here?

Update: Removing the .exe and running ILMerge with only the library works. Removing the .dll and running it with only the executable doesn't change the error output.

I also made a second .dll and was able to use ILMerge to merge the two existing libraries into one with the following:

ilmerge /targetplatform:v4,c:\Windows\Microsoft.NET\Framework\v4.0.30319 /out:Test.dll "TestDLL2.dll" "TestDLL.dll"

Update 2 So I was able to create a TestRun.exe with the following:

ilmerge /targetplatform:v4,c:\Windows\Microsoft.NET\Framework\v4.0.30319 /out:TestRun.exe "TestMain.exe" "Test.dll"

This took the two reference library .dll's packaged together and packaged them with my executable. And it worked! I was able to get the expected results.

Until I moved my new executable out of its home folder.

As soon as I moved it to a new location, execution produced this error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'TestDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

at TestMain.Program.Main(String[] args)

Upvotes: 2

Views: 5993

Answers (2)

saminpa
saminpa

Reputation: 613

[This is a repeat answer because this was the other relevant question I found relatively quickly but that didn't help me solve my issue]

I'm answering this old question because I didn't find an answer to my own related question on Stack Overflow and it might help someone else. In my case, I was trying to touch and rebuild an old solution in VS2013 to fix a glitch in the installer (not recognizing IIS10 as newer than IIS4).

The merge operation was giving me a "The system cannot find the file specified Error Code 0x80070002" and if you ignored the error and installed the application it would get stuck with a runtime exception. Of course "the file specified" was right there in the build folder.

The root cause of this problem is that frameworks after 4.0 have been an "in place upgrade" with some of the dependencies moving around the core libraries (for a long discussion see: http://www.hurryupandwait.io/blog/what-you-should-know-about-running-ilmerge-on-net-4-5-assemblies-targeting-net-4-0). The long and the short of it is that you may have several versions of ilmerge available to you but only the most recent is guaranteed to be able to accurately unshuffle your dependencies so that things will be found at runtime, it's not the files specified that are missing, it's the pointers to the underlying calls.

The solution in my case was to find a version of ilmerge retrieved from NuGet and to reference that from my old solution while leaving everything else as is. (I should have known this, but I was in a hurry/not thinking straight and the error message is misleading...).

Upvotes: 1

Sacrilege
Sacrilege

Reputation: 795

What version of the framework are you using? There is a targetplatform option you may need to set if you are using 4.0/4.5 for example.

/targetplatform:version,platformdirectory

Upvotes: 1

Related Questions