One way to do this is to use the XML configuration file and serialize it:
ConfigManager.cs
using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace MyApplication { [ Serializable() ] public class ConfigManager { private int removeDays = 7; public ConfigManager() { } public int RemoveDays { get { return removeDays; } set { removeDays = value; } } }
somewhere in your application
private ConfigManager cm;
private XmlSerializer ser;
...
Then you need to load the configuration:
private void LoadConfig() { try { cm = new ConfigManager(); ser = new XmlSerializer(typeof(ConfigManager)); filepath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + cm.filepath; if (File.Exists(filepath)) { FileStream fs = new FileStream(filepath, FileMode.Open); cm = (ConfigManager)ser.Deserialize(fs);
To save it:
XmlSerializer ser; ConfigManager cm; ... private void saveConfig() { try { cm.RemoveDays = 6; TextWriter tw = new StreamWriter(filepath, false); ser.Serialize(tw, cm); tw.Close(); } catch (Exception ex) }
user195488
source share