Updated C# Application Settings Utility Class

This is a revised version of my original post (C# Application Settings Utility Class). Thanks to a comment received I have revised this code to make the class inheritable. Please see below:

Revised code for appSettings class (note the addition of the abstract keyword in class declaration):

    public abstract class appSettings
    {
        public delegate void AppSettingsEventHander(object o, AppSettingsEventArgs e);

        // Declare Class Events
        public event AppSettingsEventHander SettingsEvent;

        private string _myPath;
        public string myPath
        {
            get { return _myPath; }
            set { _myPath = value; }
        }

        private StringCollection _myLocations;

        public StringCollection MyLocations
        {
            get { return _myLocations; }
            set { _myLocations = value; }
        }

        // Additional Application specific setting properties can be added here as needed...

        public appSettings()
        {
            InitAppSettings();
        }

        public appSettings(string sPath)
        {
            InitAppSettings();
            this.Load(sPath);
        }

        private void InitAppSettings()
        {
            this._myLocations = new StringCollection();
        }

        public bool SaveAppSettings(string thePath)
        // Returns True if Saved successfully, False otherwise.
        // Raises Event that includes exception and reason if case of error
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(this.GetType());
                FileStream fs = new FileStream(thePath, FileMode.Create);
                xs.Serialize(fs, this);
                fs.Close();
            }
            catch (FileNotFoundException fex)
            {
                AppSettingsEventArgs e = new AppSettingsEventArgs(settingsEventReason.FileNotFound, fex);
                SettingsEvent(this, e);
                return false;
            }
            catch (Exception ex)
            {
                AppSettingsEventArgs e = new AppSettingsEventArgs(settingsEventReason.OtherException, ex);
                SettingsEvent(this, e);
                return false;
            }
            return true;
        }

        public appSettings GetAppSettings(string thePath)
        {
            // Returns new appSettings object if successfull, null appSettings otherwise
            // Raises Event that includes exception and reason if case of error

            try
            {
                appSettings appSet;
                XmlSerializer xs = new XmlSerializer(this.GetType());
                FileStream fs = new FileStream(thePath, FileMode.Open);
                appSet = (appSettings)xs.Deserialize(fs);
                fs.Close();
                return appSet;
            }

            catch (FileNotFoundException fex)
            {
                AppSettingsEventArgs e = new AppSettingsEventArgs(settingsEventReason.OtherException, fex);
                SettingsEvent(this, e);
                return (appSettings)null;
                ;
            }
            catch (Exception ex)
            {
                AppSettingsEventArgs e = new AppSettingsEventArgs(settingsEventReason.OtherException, ex);
                SettingsEvent(this, e);
                return (appSettings)null;
            }
        }

        public bool Load(string thePath)
        {
            // This function loads the settings file directly to "this" instance of appSettings
            // instead of returning the appSettings Object
            // Returns true on success, false otherwise
            // Raises Event that includes exception and reason if case of error

            try
            {
                appSettings appSet;
                XmlSerializer xs = new XmlSerializer(this.GetType());
                FileStream fs = new FileStream(thePath, FileMode.Open);
                appSet = (appSettings)xs.Deserialize(fs);
                foreach (System.Reflection.PropertyInfo pi in this.GetType().GetProperties())
                {
                    if (pi.CanWrite)
                    {
                        try
                        {
                            pi.SetValue(this, pi.GetValue(appSet,null),null);
                        }
                        catch (Exception ex)
                        {
                            pi.SetValue(this, null, null);
                        }
                    }
                }
                fs.Close();
            }

            catch (FileNotFoundException fex)
            {
                AppSettingsEventArgs e = new AppSettingsEventArgs(settingsEventReason.OtherException, fex);
                SettingsEvent(this, e);
                return false;
                ;
            }
            catch (Exception ex)
            {
                AppSettingsEventArgs e = new AppSettingsEventArgs(settingsEventReason.OtherException, ex);
                SettingsEvent(this, e);
                return false;
            }
            return true;
          }

    }  //  </class appSettings>

    public class AppSettingsEventArgs : EventArgs
    {
        // Constructor
        public AppSettingsEventArgs(settingsEventReason reason, object objInfo)
        {
            this._settingsEvent = reason;
            this._objInfo = objInfo;
        }

        private object _objInfo;
        private settingsEventReason _settingsEvent;

        // Public Property(s) to return value(s)
        public object objInfo
        {
            get { return _objInfo; }
        }

        public settingsEventReason eventReason
        {
            get { return (settingsEventReason)_settingsEvent; }
        }
    }   // </ ChangeEventArgs> 

    public enum settingsEventReason
    {

        FileNotFound = 1,
        // Add additional event reasons to enum here
        OtherException = 99
    }

Here is how you would reference the revised class in your application code:

    public class appSettingsInheritedClass : appSettings
    {
    }

Using this approach, you can then simply add new application settings properties to this declaration as shown below:

    public class appSettingsInheritedClass : appSettings
    {
        public int iSetting1;
        public string sSetting2;
    }

Any properties you add in this way are automatically saved and loaded when you call SaveSettings or Load respectively.

Thanks.

G

Leave a Reply

You must login to post a comment.