views:

1326

answers:

2

I have a .NET dll which needs to read it's config settings from it's config file. Usually, the config file is placed in the same directory as the DLL. But how do i read the config file if the DLL is GAC'ed, because I can put only the DLLs in the GAC, and not it's config files.

+4  A: 

Does the user need to configure the Dll? If so, then the DLL should be using configuration settings from the app.config file, not it's own config. The app.config file should be stored in the same directory as the application. If not, then you could go a couple of different ways. You could make changes to the machine.config file so that your Dll can find them there. I would not do this. Alternatively, you can store the configuration in a settings class. These can be overridden via configuration, but your defaults will be set in the generated code for the settings class via attributes and so the absence of a configuration file will not affect your Dll when the defaults are all that are required.

tvanfosson
+2  A: 

I agree with tvanfosson the Gac'ed dll will read from the application's path. But you could also inform the dll which is the path in this way:

System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "THE PATH TO THE CONFIG";
System.Configuration.Configuration cfg =
System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);

string thevalue=cfg.AppSettings.Settings[variable].Value;
netadictos