Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading dll.config (not app.config!) from a plugin module

I am writing a C# .NET 2.0 .dll that is a plug in to a Larger application. The visual studio project for my module has a app.config file which is copied to a MyProj.dll.config along side of MyProj.dll.

The plan is that MyProj.dll.config will be edited after the .dll is deployed. I am trying to read my settings from that modified local file. I have tried pulling out the LocalFilesSettingsObject and changing it's application name to my .dll like this:

        Properties.Settings config = Properties.Settings.Default;
        SettingsContext context = config.Context;
        SettingsPropertyCollection properties = config.Properties;
        SettingsProviderCollection providers = config.Providers;
        SettingsProvider configFile = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
        configFile.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
        config.Initialize(context, properties, providers);
        config.Reload();

That is not working. I am struggling to wrap my head around the whole .NET Settings mess. I'd like a recipe to finish this task. I would also like a link to a clear explanation (with examples) of how settings are supposed to work in .NET 2.0

like image 343
Rodney Schuler Avatar asked Jul 30 '09 19:07

Rodney Schuler


People also ask

Can a DLL have app config file?

You can have separate configuration file, but you'll have to read it "manually", the ConfigurationManager. AppSettings["key"] will read only the config of the running assembly. This will add App. config to the project folder, put your settings in there under <appSettings> section.

Can class library have app config?

Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App. config file. Even if you distribute an App. config file alongside your library's assembly file, the library code will not read the file.


2 Answers

You will need to load the x.dll.config (with the Configuration API) yourself. All the automatic file handling (including the .Settings) is all about machine.config/y.exe.config/user-settings.

To open a named config file:

  • Reference System.Configuration.dll assembly.
  • Using System.Configuration
  • Create code like:

    Configuration GetDllConfiguration(Assembly targetAsm) {
      var configFile = targetAsm.Location + ".config";
      var map = new ExeConfigurationFileMap {
        ExeConfigFilename = configFile
      };
      return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    }
    
like image 132
Richard Avatar answered Sep 27 '22 20:09

Richard


1- open app.config file in visual studio

2- in the "configuration" tag add your configurations in tag "appSettings" as bellow:

<configuration>
    <appSettings>
        <add key="UserName" value="aaa"/>
        <add key="Password" value="111"/>
    </appSettings>
</configuration>

3- in your code c#

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string userName = appConfig.AppSettings.Settings["UserName"].Value;
string password = appConfig.AppSettings.Settings["Password"].Value;

and do not forgot to add this 2 usings for "ConfigurationManager" and for "Assembly"

  • using System.Configuration;
  • using System.Reflection;

if the System.Configuration does not show, you must add the reference "System.Configuration" in the References

4- you can update the configurations for the dll as bellow:

  • open the file "MyProj.dll.config"
  • then update your configurations
like image 36
Mohamad Chami Avatar answered Sep 27 '22 21:09

Mohamad Chami