Οκ, πεντάλεπτο παράδειγμα ενός custom config section, με τον πιο εύκολο δυνατό τρόπο ... ένα απλό hashtable, ακριβώς όπως κάνει και το appSettings.
1. Ορίζεις το custom section στο <configSections> μέρος του App/Web.config σου:
<configSections>
...
<section name="br_ActivityNotifier" type="System.Configuration.DictionarySectionHandler,system,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null"/>
</configSections>
To name attribute ορίζει το xml element του custom config section.
2. Γράφεις το custom xml σου στο section που όρισες προηγουμένως:
<br_ActivityNotifier>
<add key="templates_folder"
value="c:\inetpub\wwwroot\auditorcis_15\resources\files\activity-templates\"/>
<add key="Start" value="generic-template.htm"/>
<add key="Finalize" value="generic-template.htm"/>
<add key="UndoFinalize"
value="generic-template.htm"/>
<add key="Cancel" value="generic-template.htm"/>
<add key="UndoCancel" value="generic-template.htm"/>
<add key="Assign" value="assign-template.htm"/>
</br_ActivityNotifier>
H σύνταξή του όπως παρατηρείς είανι ακριβώς ίδια με του appSettings.
3. Χρησιμοποιείς τη μέθοδο παρακάτω για να πάρεις key values απ'το section σου:
public static
string GetAppSettingFromSectionWithKey(string section, string
key)
{
try
{
Hashtable
settings =
(ConfigurationSettings.GetConfig(section) as
Hashtable);
if(settings.ContainsKey(key)) return(settings[key].ToString());
else throw new ConfigurationAccessException("Key " +
key + " was not found");
}
catch(Exception ex)
{
//TODO:check if key is null - this will fail as well...
throw new
ConfigurationAccessException("Error while trying to retrieve the " +
key + " from section " + section +" configuration file.\nSystem
Message:" + ex.Message,ex);
}
}
..και et voila :) Ένα custom section μέσα στο config σου.
Πολύ καλή μας μέρα !
Angel
O:]