CustomSettingsProvider : CSharp

SettingsProvider ia an abstract base class in .net framework that provides base framework in dealing with application settings. To create custom implementation of application settings, you must override this base class. 

There is already a default implemenation of SettingsProvider in .net framework that stores user settings in local user app directory. 

The following is a complete implementation of CustomSettingsProvider that stores use settings file in common app data folder and can be shared between different users. 

Open the settings window, click on the property for which you want to apply custom settings provider. Then in properties box set the ProviderName to CustomSettingsProvider. (or your own class name). 



 public class CustomSettingsProvider : SettingsProvider
    {
        private const string Settingsroot = "Settings";

        public override void Initialize(string name, NameValueCollection col)
        {
            base.Initialize(this.ApplicationName, col);
        }

        public override string ApplicationName
        {
            get { return "MyAppName"; }
            set { }
        }

        public override string Name
        {
            get { return "CustomSettingsProvider"; }
        }
        public virtual string GetAppSettingsPath()
        {

            string mainpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyCompanyName");
            string path = Path.Combine(mainpath, ApplicationName);
            return path;

        }

        public virtual string GetAppSettingsFilename()
        {
            return ApplicationName + ".settings";
        }

        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
        {
            foreach (SettingsPropertyValue propval in propvals)
            {
                SetValue(propval);
            }
            try
            {
                SettingsXml.Save(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
            }
            catch (Exception)
            {
            }

        }


        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
        {
            var values = new SettingsPropertyValueCollection();
            foreach (SettingsProperty setting in props)
            {
                var value = new SettingsPropertyValue(setting) {IsDirty = false, SerializedValue = GetValue(setting)};
                values.Add(value);
            }
            return values;
        }

        private XmlDocument settingsXml = null;
        private XmlDocument SettingsXml
        {
            get
            {
                if (settingsXml == null)
                {
                    settingsXml = new XmlDocument();
                    try
                    {
                        settingsXml.Load(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
                    }
                    catch (Exception ex)
                    {
                        XmlDeclaration dec = settingsXml.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
                        settingsXml.AppendChild(dec);
                        XmlNode nodeRoot = default(XmlNode);
                        nodeRoot = settingsXml.CreateNode(XmlNodeType.Element, Settingsroot, "");
                        settingsXml.AppendChild(nodeRoot);
                    }
                }

                return settingsXml;
            }
        }

        private string GetValue(SettingsProperty setting)
        {
            string ret = "";

            try
            {
                ret = IsRoaming(setting) ? SettingsXml.SelectSingleNode(Settingsroot + "/" + setting.Name).InnerText 
                    : SettingsXml.SelectSingleNode(Settingsroot + "/" + Environment.MachineName + "/" + setting.Name).InnerText;
            }

            catch (Exception ex)
            {

                ret = (setting.DefaultValue != null) ? setting.DefaultValue.ToString() : "";
            }

            return ret;
        }

        private void SetValue(SettingsPropertyValue propVal)
        {
            XmlElement settingNode = default(XmlElement);

            
            try
            {
                if (IsRoaming(propVal.Property))
                {
                    settingNode = (XmlElement)SettingsXml.SelectSingleNode(Settingsroot + "/" + propVal.Name);
                }
                else
                {
                    settingNode = (XmlElement)SettingsXml.SelectSingleNode(Settingsroot + "/" + Environment.MachineName + "/" + propVal.Name);
                }
            }
            catch (Exception ex)
            {
                settingNode = null;
            }

            // if the node exists, if so then set its new value
            if ((settingNode != null))
            {
                settingNode.InnerText = propVal.SerializedValue != null ? propVal.SerializedValue.ToString() : "";
            }
            else
            {
                if (IsRoaming(propVal.Property))
                {
                    settingNode = SettingsXml.CreateElement(propVal.Name);
                    settingNode.InnerText = propVal.SerializedValue.ToString();
                    SettingsXml.SelectSingleNode(Settingsroot).AppendChild(settingNode);
                }
                else
                {
                    XmlElement machineNode = default(XmlElement);
                    try
                    {

                        machineNode = (XmlElement)SettingsXml.SelectSingleNode(Settingsroot + "/" + Environment.MachineName);
                    }
                    catch (Exception ex)
                    {
                        machineNode = SettingsXml.CreateElement(Environment.MachineName);
                        SettingsXml.SelectSingleNode(Settingsroot).AppendChild(machineNode);
                    }

                    if (machineNode == null)
                    {
                        machineNode = SettingsXml.CreateElement(Environment.MachineName);
                        SettingsXml.SelectSingleNode(Settingsroot).AppendChild(machineNode);
                    }

                    settingNode = SettingsXml.CreateElement(propVal.Name);
                    settingNode.InnerText = propVal.SerializedValue != null ? propVal.SerializedValue.ToString() : "";
                    machineNode.AppendChild(settingNode);
                }
            }
        }

        private bool IsRoaming(SettingsProperty prop)
        {
            return (from DictionaryEntry d in prop.Attributes select (Attribute) d.Value).OfType<SettingsManageabilityAttribute>().Any();
        }
    }
Advantages of custom settings provider
  • User settings can be stored on your favourate location. 
  • Users can share the same settings. 
  • User settings are preserved when application is upgraded.