How do I use, User settings and Custom Configuration Sections in app.config?
<mySection id="myId">
<item1 data="info">
<item2 data="more info">
</mySection>
And link them to a type.
app.config
Where do key value pairs go?
appSettings
How? give example
<add key="File_Count" value="2" />
.cs
What class gives access to user settings?
System.Configuration.ConfigurationManager
What must the Project reference?
System.Configuration
Which method gives the basic settings? Example for the above
AppSettings["File_Count"]
Given the following custom section
<mySection id="myId">
<item1 data="info">
<item2 data="more info">
</mySection>
What is needed to declare this in the app file, for a class type called "Sample.myType" in "myAssembly"?
<configSections>
<section name="mySection" type="Sample.myType, myAssembly" />
</configSections>
What is the mapping of xml element mapping to c# attributes?
mySection ConfigurationSection
item Type ConfigurationElement
What is the mapping of xml property to c# attribute ?
id ConfigurationProperty
data ConfigurationProperty
item1 ConfigurationProperty
item2 ConfigurationProperty
How to declare the class for the type 'myType'?
public class myType : ConfigurationSection {
How to declare a simple property 'id'?
//Inside myType as
[ConfigurationProperty("id", IsRequired = false)]
public string Id {
get { return (string)this["id"]; }
set { this["id"]=value;}
}
How to declare the type for the item elements?
//Inside myType as a sub class
public class myTypeElement : ConfigurationElement {
[ConfigurationProperty("data", IsRequired = false)]
public string Data {
get { return (string)this["data"]; }
set { this["data"]=value;}
}
}
How to declare the item elements?
[ConfigurationProperty("item1)]
public myTypeElement Item1{
get { return (myTypeElement)this["item1"] }
set { this["item1"]=value;}
}
How to access these from the group name "mySection"?
Sample.myType t = (Sample.myType)System.Configuration.ConfigurationManager.GetSection("mySection");
string s = t.item1.data;
Where is this in MSDN, and others?
How to: Create Custom Configuration Sections Using ConfigurationSection
msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
blog.danskingdom.com/adding-and-accessing-custom-sections-in-your-c-app-config/