Wednesday, May 25, 2011

Using Custom section in app.config for Visual Studio 2010

Sample app.config:
<configuration>
<configSections>
<sectionGroup name="machineSettings">
<section name="apple" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>

<appSettings>
<add key="TestMode" value="false" />
<add key="sdpBatPath" value="e:\sdpack\sdp.bat" />
</appSettings>

<machineSettings>
<apple>
<add key="myKeyA" value="AAA"/>
<add key="myKeyB" value="BBB"/>
</apple>
</machineSettings>

Code:
// NOTE: this is a case sensitive lookup
NameValueCollection userConfig = ConfigurationManager.GetSection("machineSettings/" + System.Environment.MachineName.ToLower()) as NameValueCollection;


Trouble Shooting:

Could not find schema information for the attribute 'key'
This message can be resolved by changing the schema for the app.config:

  • Open Properties window
  • Open app.config in Visual Studio
  • Goto Properties window
  • Select Schemas
  • Edit the Schemas
  • Choose /add a new schema
  • In my chase adding DotNetConfig20.xsd worked.

No comments:

Post a Comment