views:

990

answers:

2

I am accessing my assembly's configuration like this:

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
AppSettingsSection appSettings = conf.AppSettings;

My .config file contains a section like this

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="CsDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<connectionStrings>
    <add name="CsDll.Properties.Settings.SabreCAD" connectionString="A Connection string." />
    <add name="CsDll.Properties.Settings.StpParts" connectionString="Another connection string" />
</connectionStrings>
 <applicationSettings>
        <CsDll.Properties.Settings>
            <setting name="StpInsertSearchPath" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
                        <string>A string</string>
                        <string>Another string in the collection</string>

I can successfully read the connection strings including changes if I edit the .config file. So, I know I am connected to the correct file. But I cannot find that string collection inside of the appSettings object. It is is not in the .Settings KeyValueConfigurationCollection. Where do I find my string collection?

+1  A: 

You connection strings are typically inside of the configuration manager's ConnectionStrings property. You should be able to access in a much simpler fashion through its static method.

string myConnectionString = ConfigurationManager.ConnectionStrings["connectioStringName"];

I believe you should use the tag "AppSettings" instead of "ApplicationSettings" in the .config file to enable the ConfigurationManager to access through the AppSettings property.

I don't know enough about how ConfigurationManager works to be sure this will solve your problem, but renaming it and removing that custom section group should allow AppSettings to work properly.

Edit Yes, it appears that ConfigurationManager's AppSettings property accesses the section named in the .config file.

jkelley
+3  A: 

You should be access the items in the collection using this simpler syntax

foreach (string s in CsDll.Properties.Settings.Default.StpInsertSearchPath)
{
    Console.WriteLine(s);
}

EDIT:

The following code should do the trick

ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"; 
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
ConfigurationSectionGroup appSettingsGroup = conf.GetSectionGroup("applicationSettings");
ClientSettingsSection clientSettings = (ClientSettingsSection) appSettingsGroup.Sections["CsDll.Properties.Settings"];
ConfigurationElement element = clientSettings.Settings.Get("StpInsertSearchPath");
string xml = ((SettingElement)element).Value.ValueXml.InnerXml;
XmlSerializer xs = new XmlSerializer(typeof(string[]));
string[] strings = (string[])xs.Deserialize(new XmlTextReader(xml, XmlNodeType.Element, null));
foreach (string s in strings)
{
    Console.WriteLine(s);
}

There may be a shorter way, but this works for me.

Michael McCloskey
OK. I can read the string array that way. But, the strings are coming from the default compiled into the assembly. I need to be able to add, remove and change strings in that collection after the assembly is deployed. The .Properties.Settings.Default does not pick up edits to the .config file.Had the same problem with the connection strings until I started going through the ConfigurationManager.
rschuler
You probably need to access the ConfigurationManager's method:ConfigurationManager.GetSection("applicationSettings");which should return the object for which you are looking to analyze
jkelley
ConfigurationManager.GetSection("applicationSettings") returns null
rschuler
Please refer to my edit. It finagles its way to the appropriate XML element in the config file, then deserializes it using standard Xml serialization calls.
Michael McCloskey