Prev: SSRS 2008, C# and ReportingService.Render()
Next: the item property of the ConfigurationSectionCollection doesn'twork
From: Tony Johansson on 23 Feb 2010 06:12 Hi! In this program I add a custom section called LabSection that inherit from ConfigurationSection to the configuration file. When I run the program from the command line prompt the new custom section LabSection is added to the configuration file which is correct. But when I run from within VS everything seems to work perfectly except that the configuration file is now updated. So my first question is why does it work perfect from the command line prompt but not from within VS ? My second question is class Program { private static void WriteSettings() { LabSection labSec; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.Sections["LabSection"] == null) { labSec = new LabSection(); config.Sections.Add("LabSection", labSec); labSec.SectionInformation.ForceSave = true; labSec.FirstName = "Karl"; labSec.LastName = "Otto"; config.Save(ConfigurationSaveMode.Full); ConfigurationSectionCollection csc = config.Sections; foreach(string s in config.Sections.Keys) { Console.WriteLine(s); } } } static void Main(string[] args) { WriteSettings(); } } public class LabSection : ConfigurationSection { [ConfigurationProperty("LastName", IsRequired = false, DefaultValue = "NotGiven")] public string LastName { get { return (string)base["LastName"]; } set { base["LastName"] = value; } } [ConfigurationProperty("FirstName", IsRequired = false, DefaultValue = "NotGiven")] public string FirstName { get { return (string)base["FirstName"]; } set { base["FirstName"] = value; } } } <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Foo" value="Hello World"/> </appSettings> </configuration> You can use an object of the ConfigurationSectionCollection class to read all the configuration sections in a configuration file. The ConfigurationSectionCollection class consists of the objects of the ConfigurationSection class. The ConfigurationSectionCollection class items can be accessed through the collection iterator. You can use the item property of the ConfigurationSectionCollection class to access the objects of the ConfigurationSection class. |