I'm developing an app which stores some different user settings in app.config such as connection strings, paths for saving reports, default printer e.t.c
For retrieving them I made a static class StoredSettingsTools
which contains an Enum
of settings types and a public method which calls (depending of setting type) some private method and get the settings from app.config
:
public static class StoredSettingsTools
{
public enum StoredSettingsType
{
DBConnection,
Printer,
ReportsFolder
};
private static ConnectionStringsSection GetConfigurationConnectionStringsSection()
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConnectionStringsSection conectionSettings = (ConnectionStringsSection)config.SectionGroups.Get("UserSettingsGroup")
.SectionGroups.Get("StoredConnectionSettings")
.Sections.Get("defaultConnectionString");
return conectionSettings;
}
private static Hashtable GetConfigurationPrinterSection()
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConfigurationSectionGroup userSettingsGroup = config.SectionGroups["UserSettingsGroup"];
Hashtable sectionSettings = ConfigurationManager.GetSection(userSettingsGroup.Sections.Get("printerSettings").SectionInformation.SectionName) as Hashtable;
return sectionSettings;
}
private static Hashtable GetConfigurationReportsFolderSection()
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConfigurationSectionGroup userSettingsGroup = config.SectionGroups["UserSettingsGroup"];
Hashtable sectionSettings = ConfigurationManager.GetSection(userSettingsGroup.Sections.Get("reportsFolderSettings").SectionInformation.SectionName) as Hashtable;
return sectionSettings;
}
public static Hashtable GetStoredSettings(StoredSettingsType sectionType)
{
Hashtable table = new Hashtable();
switch (sectionType)
{
case StoredSettingsType.DBConnection:
{
ConfigurationSection section = GetConfigurationConnectionStringsSection();
ConnectionStringSettingsCollection col = ((ConnectionStringsSection)section).ConnectionStrings;
foreach (ConnectionStringSettings settings in col)
{
table.Add(settings.Name, settings.ConnectionString);
}
return table;
}
case StoredSettingsType.Printer:
{
table = GetConfigurationPrinterSection();
return table;
}
case StoredSettingsType.ReportsFolder:
{
table = GetConfigurationReportsFolderSection();
return table;
}
}
return null;
}
}
Is anything wrong with this code? Could it be more object-oriented?