public static void SaveConfig(CustomSection customSection) { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None) as Configuration; if (config.GetSection("CustomSection") == null) { config.Sections.Add("CustomSection", customSection); } else { var section = config.GetSection("CustomSection") as CustomSection; section.Port = customSection.Port; } config.Save(); } catch (ConfigurationErrorsException err) { Console.WriteLine("Using SaveConfig(CustomSection): {0}", err.ToString()); } }
public static CustomSection GetCustomSection() { try { CustomSection customSection; System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None) as Configuration; customSection = config.GetSection("CustomSection") as CustomSection; if (customSection == null) { customSection = new CustomSection(); } Console.WriteLine("COM Port: {0}", customSection.Port); Console.WriteLine("Period: {0}", customSection.Period); return(customSection); } catch (ConfigurationErrorsException err) { Console.WriteLine("Using GetSection(string): {0}", err.ToString()); } return(null); }