0

I want to force the compiled program to use the dll from the %temp% folder.

However, if I do like that

compars.EmbeddedResources.Add(Path.GetTempPath() + "\\Newtonsoft.Json.dll");
compars.ReferencedAssemblies.Add(Path.GetTempPath() + "\\Newtonsoft.Json.dll");

The program crashes.

I have also added using Newtonsoft.Json.dll but I still need to have the Newtonsoft.Json.dll in the same folder as my program, if so, the program isn't crashing, otherwise it crashes even if Newtonsoft.Json.dll is in the %temp% folder. So in general, I want to run the program successfully without having the dll in the same folder, and force it to use the dll from the %temp% or something like that.

  • Does this answer your question? [How to add folder to assembly search path at runtime in .NET?](https://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net) – Rand Random Jan 24 '22 at 15:46

2 Answers2

0

You can embed other dlls into your program easier. Please refer to the following link:

stackoverflow.com/questions

You can also use ILMerge to do it automatically.

You can also surround your code with try-catch to get more detailed information. I guess your dll's version is not equal or some similar issues.

0

Adding the referenced assemblies to the compiler parameters (I assume that is what compars is referring to) will make sure the compiler uses that dll as reference at compile-time. During execution of the compiled program, the operating system will try to locate the referenced assemblies in the "usual way".

There are so many detailed specifications on this that you could read them 24/7 until your death (e.g. https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order).

But to accomplish what you want, namely to have the compiled program use the specific dll from your temp folder at run-time, you can merge it with ILMerge as suggested in Mostafa Khodakarami's answer. The library will become an integral part of yours. Your dll will not be at risk of failing to find the needed reference, but it will also not use a newer version of the referenced library if one is available. Depending on how you do the merging, you also might not be able to share objects created with the library with other third party libraries that also use it, unless you also merge them.

Another tool to do the merging is ILRepack (see https://github.com/gluck/il-repack) which touts itself as being better than ILMerge. We switched from ILMerge to ILRepack because we reached some kind of internal limit on the number of objects in ILMerge.

Finally, you could also have your program load the dll itself "manually" by using

public static System.Reflection.Assembly LoadFrom (string assemblyFile);

(see https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.loadfrom?view=net-6.0)

Call the method like this at the beginning of your program, before any methods are called or objects created that use that library:

Assembly.LoadFrom(Path.GetTempPath() + "\\Newtonsoft.Json.dll");
Christopher Hamkins
  • 1,442
  • 9
  • 18