locked
How do I access ApplicationSettings section from another assembly RRS feed

  • Question

  • Hi,

    I'm having trouble acessing a setting I just created in my application configuration file. Two important things about what I'm trying to do:

    1. I'm trying to access the configuration from a different class library project named Main.dll, not the exe project that the configuration file belongs to named Shell.exe. I don't want to reference Shell.exe from Main.
    2. I'm referring to the <applicationSettings> group in my config file. Not <appSettings>. Normally you would access the setting like this Shell.Properties.Settings.Default.EnableMSRLogging but since I'm not referencing the project, I need to get to it directly through the config file (and I understand it won't be strongly typed).

    I've been poking around System.Configuration and System.ConfigurationManager and cannot find a way to get to my new setting! The setting is below, how can I access it? Thanks!

    <applicationSettings> 
     <Shell.Properties.Settings> 
       <setting name="EnableMSRLogging" serializeAs="String">  
         <value>True</value> 
       </setting> 
     </Shell.Properties.Settings> 
    </applicationSettings> 

     

    Monday, February 9, 2009 4:47 PM

Answers

  • It's not pretty but I got it! I needed 2 key pieces of information:
    1. When loading a configuration file, you don't add the .config extention to the filename. If you do, you end up loading YouApp.exe.config.config.
    2. The piece of the config file I'm trying to access is represented by the ClientSettingsSection class which inherits from ConfigurationSection.
    using System.Configuration;
    ...


    //Get an instance of my configuration
     
    Configuration cfg = ConfigurationManager.OpenExeConfiguration("MyApplication.exe");  
     
    //Get my settings section from the applicationSettings SectionGroup  
    ClientSettingsSection settingsSection = (ClientSettingsSection)cfg.GetSectionGroup("applicationSettings").Sections["Shell.Properties.Settings"];  
     
    //Get my specific setting from my settings section  
    SettingElement mySetting = settingsSection.Settings.Get("EnableMSRLogging");  
     
    //Get the value of the setting  
    bool enableLogging = mySetting.Value.ValueXml.InnerText == Boolean.TrueString; 

    I'd love to see an easier way if anyone knows of one.
    • Marked as answer by _xr280xr_ Monday, February 9, 2009 8:45 PM
    Monday, February 9, 2009 8:45 PM