How to load dll configuration file in another appdomain

I have an application that needs to download the add-in as a dll. The DLL should get the configuration information from the configuration file (app.config). I want to dynamically find out the name of the app.config file, and the way to do this, as I understand it, is AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

However, since it is hosted in the parent INSIDE application, the configuration file obtained from the above code fragment is (parentapplication) .exe.config. I cannot load another appdomain inside the parent application, but I would like to modify the data of the appdomain configuration file. How do I do this to get the dll configuration file?

+4
source share
1 answer

OK, in the end, I managed to hack something together that works for me. Perhaps this will help;

Using Assembly.GetExecutingAssembly, from a DLL that has a configuration file that I want to read, I can use .CodeBase to find where the DLL was before I launched the new AppDomain for this. * .dll .config is in the same folder.

Then you need to convert the URI (as .CodeBase looks like "file: //path/assembly.dll") to get the LocalPath for the ConfigurationManager (which does not like Uri formatted strings).

try { string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; string originalAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); Uri uri = new Uri(String.Format("{0}\\{1}.dll", originalAssemblyPath, assemblyName)); string dllPath = uri.LocalPath; configuration = ConfigurationManager.OpenExeConfiguration(dllPath); } catch { } 
+3
source

All Articles