1

So I have made an updater for my application. It downloads my application (a .dll file) tp %appdata%/Folder and opens it via reflection like this:

var appdataFolder =
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\[ApplicationName]\";

var mainAssembly = Assembly.LoadFrom(appdataFolder + @"TF2Callout.dll");
mainAssembly.GetType("TF2Callout.App").GetMethod("Main").Invoke(null, null);

This opens the .dll and executes the static void Main. But in my application I have this:

Assembly.LoadFrom("SomeOtherDll.dll");

I would expect the application to search for the .dll in the %appdata%/[application name] folder because it is saved there, however, it tries to find the file in the folder of the installer, which is in my program files.

How would I go about fixing this? I would rather not make the SomeOtherDll.dll get loaded from an absolute path, because it might change.

Tvde1
  • 1,246
  • 3
  • 21
  • 41
  • 1
    You will have to use absolute path. The framework will search in executable folder if no specific path is mentioned. That's the default behavior. – Pratik Gaikwad Jul 04 '17 at 14:03
  • Really no way to make it relative to the Application's dll? – Tvde1 Jul 04 '17 at 14:06
  • You must have *some* idea where "application's dll" is stored. Putting the "updater" executable in the same directory as the app would be a wise idea, now you can use Assembly.GetEntryAssembly().Location – Hans Passant Jul 04 '17 at 14:26

1 Answers1

0

Assembly.LoadFrom behaves as specified at https://msdn.microsoft.com/en-us/library/1009fa28(v=vs.110).aspx :

assemblyFile may be absolute or relative to the current directory, and the assembly is loaded into the domain of the caller.

https://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.110).aspx has details about what current directory means.

If you wish to load from a specific location, you must use an absolute path. You may be able to construct that absolute path by using appdataFolder or checking the path of an existing DLL (e.g. How to get the location of the DLL currently executing? ). But fundamentally, if you want the DLL to load reliably, you need to use an absolute path.

mjwills
  • 23,389
  • 6
  • 40
  • 63