I have read a good 15+ articles and tutorials on creating custom configuration sections, and I can't seem to 'get' the custom configuration. ANY help would be appreciated, since I am clearly missing something. It is a simple program to schedule backups and specify a list of source/destination directories.
I am receiving the error: Unrecognized element 'add'. (BattistoneBackup.exe.Config line 8)
My App.config file looks like this:
<?xml version="1.0"?> <configuration> <configSections> <section name="backupDirectories" type="Battistone.WindowsService.BattistoneBackup.BackupDirectorySection, BattistoneBackup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> <section name="backupSchedule" type="Battistone.WindowsService.BattistoneBackup.BackupScheduleSection, BattistoneBackup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </configSections> <backupDirectories> <add Name="DEV_Sports_Source" SourceDirectory="C:\Dustin\Development\Visual Studio\Projects\Battistone\Sports\" DestinationDirectory="C:\Dustin\BACKUPTEST\Development\Visual Studio\Projects\Battistone\Sports\"/> <add Name="DEV_Builds" SourceDirectory="C:\Dustin\Development\Visual Studio\Projects\Battistone\Builds\" DestinationDirectory="C:\Dustin\BACKUPTEST\Development\Visual Studio\Projects\Battistone\Builds\"/> </backupDirectories> <BackupSchedule> <add BackupFrequency="Daily" BackupDays="Mon" BackupTime="23:00:00" /> </BackupSchedule> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/> </startup> </configuration>
|
I am using the following line of code to retrieve the configuration:
BackupDirectorySection oBackupDirectoriesConfigurationSection = BackupDirectorySection.GetConfig();
|
Here are the classes to wrap the configuration above (all inside the "Battistone.WindowsService.BattistoneBackup" namespace):
public class BackupDirectorySection : ConfigurationSection { public static BackupDirectorySection GetConfig() { return ConfigurationManager.GetSection("backupDirectories") as BackupDirectorySection; }
[ConfigurationProperty("backupDirectories")] public BackupDirectoryConfigurationCollection BackupDirectories { get { return this["backupDirectories"] as BackupDirectoryConfigurationCollection; } } }
public class BackupDirectoryConfigurationCollection : ConfigurationElementCollection { public BackupDirectoryConfiguration this[int index] { get { return base.BaseGet(index) as BackupDirectoryConfiguration; } set { if (base.BaseGet(index) != null) { base.BaseRemoveAt(index); } this.BaseAdd(index, value); } }
protected override ConfigurationElement CreateNewElement() { return new BackupDirectoryConfiguration(); }
protected override object GetElementKey(ConfigurationElement element) { return ((BackupDirectoryConfiguration)element).Name; } }
public class BackupDirectoryConfiguration : ConfigurationElement { [ConfigurationProperty("Name")] public string Name { get { return (string)this["Name"]; } set { this["Name"] = value; } }
[ConfigurationProperty("SourceDirectory")] public string SourceDirectory { get { return (string)this["SourceDirectory"]; } set { this["SourceDirectory"] = value; } }
[ConfigurationProperty("DestinationDirectory")] public string DestinationDirectory { get { return (string)this["DestinationDirectory"]; } set { this["DestinationDirectory"] = value; } } }
|