public bool SetValue(string preference, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                if (!DefaultPreferences.TryGetValue(preference, out string defaultValue))
                {
                    Preferences.Remove(preference);
                }
                else
                {
                    Preferences[preference] = defaultValue;
                }
            }
            else
            {
                Preferences[preference] = value;
            }

            return(WritePreferences(Preferences, DefaultPreferences));
        }
示例#2
0
        public bool SavePreferences()
        {
            List <string> lines = new List <string>();

            foreach (KeyValuePair <string, string> entry in Preferences.OrderBy(x => x.Key))
            {
                //If the value didn't exist in the defaults or the value's different, include it in the user's preferences file
                if (!DefaultPreferences.TryGetValue(entry.Key, out string value) || !string.Equals(value, entry.Value, StringComparison.Ordinal))
                {
                    lines.Add($"{entry.Key}={entry.Value}");
                }
            }

            try
            {
                File.WriteAllLines(PrefsFilePath, lines);
                return(true);
            }
            catch
            {
                return(false);
            }
        }