none
c# save settings to text file (solved) RRS feed

  • General discussion

  • To save settings of you application and simply edit later their.

    Usage:

                this.Left = Settings.Get("FormMainLeft", 0);
                this.Top = Settings.Get("FormMainTop", 0);
                this.TopMost = Settings.Get("checkBoxMainFormTopmost", false); 
                Settings.Set("FormMainLeft", this.Left);
                Settings.Set("FormMainTop", this.Top);

    code:

    using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Reflection; 
    using System.Text; 
    using System.Windows.Forms;
    using System.Xml; 
    using System.Xml.Serialization;
    
    [Serializable]
    public static class Settings
    {
        private static List<SettingsItem> settingsList = new List<SettingsItem>();
        private static string settingsPath = Application.StartupPath.ToString() + "\\settings.txt";
        public static String Get(String name, String defVal)
        {
            ReloadSettings();
            SettingsItem findSet = settingsList.Find(x => x.Name == name);
            if (findSet != null)
            {
                return findSet.Value;
            }
            else
            {
                settingsList.Add(new SettingsItem(name, defVal));
                SaveSettings();
                return defVal;
            }
        }
        public static Boolean Get(String name, Boolean defVal)
        {
            ReloadSettings();
            SettingsItem findSet = settingsList.Find(x => x.Name == name);
            if (findSet != null)
            {
                try 
                {
                    return Boolean.Parse(findSet.Value); 
                }
                catch 
                { 
                    return defVal; 
                }
            }
            else
            {
                settingsList.Add(new SettingsItem(name, defVal.ToString()));
                SaveSettings();
                return defVal;
            }
        }
        public static int Get(String name, int defVal)
        {
            ReloadSettings();
            SettingsItem findSet = settingsList.Find(x => x.Name == name);
            if (findSet != null)
            {
                try { return int.Parse(findSet.Value); }
                catch { return defVal; }
            }
            else
            {
                settingsList.Add(new SettingsItem(name, defVal.ToString()));
                SaveSettings();
                return defVal;
            }
        }
        public static void Set(String name, String val)
        {
            SettingsItem findSet = settingsList.Find(x => x.Name == name);
            if (findSet == null)
            {
                settingsList.Add(new SettingsItem(name, val));
                SaveSettings();
            }
            else
            {
                findSet.Value = val;
                SaveSettings();
            }
        }
        public static void Set(String name, Boolean val)
        {
            SettingsItem findSet = settingsList.Find(x => x.Name == name);
            if (findSet == null)
            {
                settingsList.Add(new SettingsItem(name, val.ToString()));
                SaveSettings();
            }
            else
            {
                findSet.Value = val.ToString();
                SaveSettings();
            }
        }
        public static void Set(String name, int val)
        {
            SettingsItem findSet = settingsList.Find(x => x.Name == name);
            if (findSet == null)
            {
                settingsList.Add(new SettingsItem(name, val.ToString()));
                SaveSettings();
            }
            else
            {
                findSet.Value = val.ToString();
                SaveSettings();
            }
        }
        public static void SaveSettings()
        {
            string[] settings = new string[settingsList.Count];
            int idx=0;
                foreach (SettingsItem setting in settingsList)
                {
                    settings[idx] = setting.Name + "=" + setting.Value;
                    idx++;
                }
                File.WriteAllLines(settingsPath,settings);
         }
         public static void ReloadSettings()
         {
            if (File.Exists(settingsPath))
            {
                settingsList.Clear();
                string[] settings = File.ReadAllLines(settingsPath);
                foreach (string line in settings)
                {
                    string[] lineArray = line.Split('=');
                    settingsList.Add(new SettingsItem(lineArray[0], lineArray[1]));
                }
            }
         }
         public class SettingsItem
         {
             public String Name { get; set; }
             public String Value { get; set; }
             public SettingsItem()
             {
             }
             public SettingsItem(string name, string value)
             {
                 this.Value = value;
                 this.Name = name;
             }
         }




    • Edited by vitalinvent Tuesday, December 24, 2013 11:21 AM
    Tuesday, December 24, 2013 11:19 AM

All replies

  • Have any simple solution?
    Tuesday, December 24, 2013 11:20 AM
  • vitalinvent,

    Sorry but you have posted to a forum that deals exclusively with questions/issues about customizing and programming Microsoft Project, a planning and scheduling application. I suggest you delete this post and find a more appropriate forum.

    John

    Tuesday, December 24, 2013 5:27 PM