Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Share User Settings Between Applications

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
17 Dec 2008CPOL5 min read 68.6K   634   46   24
A not-so-dotNet way to allow multiple programs to share a single settings file
Image 1

Introduction

In the process of writing another article scraping application, I came to need the ability to share user settings between two applications. I looked around and came to the conclusion that all of the solutions available that used the built-in Settings functionality in the .Net framework were a) cumbersome, and b) just plain overly difficult to implement, resulting in obscure, dodgy-looking and difficult to maintain code. Granted, I didn't look around too hard, but what I found was enough to convince me to roll my own solution.

This project requires the use of Visual Studio 2008 and dotNet 3.5 (or higher).

The Basic Idea

I decided that I should build a separate class library so that I would only have to write the settings-handling code one time, and just use that assembly in the various applications that needed access to the settings file. I wanted to come up with a base class that handled most of the drudgery, and leave the programmer with as little required work as possible.>/p>

Further, I wanted to provide "default setting" functionality so that your app could specify default settings that could be retrieved in the event the user need them. Lastly, I wanted to make the whole thing as self-sufficient as possible, so there are a couple of utility methods included that make the programmers life a little easier.

The settings are stored in a XML file in the specified (special) folder, and I use System.Xml.Linq to load/save the file and to transfer data between the settings objects (that's why it needs dotNet 3.5).

The AppSettingsBase Class

The AppSettingsBase class provides the bulk of the functionality for the library and is intended to be derived from. The original version of this code/article pointed out that the class was abstract even though there were no abstract methods in it. Navaneenth (a user here at CP) pointed out that I could achieve the desired goal of preventing instantiating of the class by making the constructor protected instead of public. The article and code now reflect this comment.

First, we have the data members and properties. There's really nothing remarkable about this block of code with the exception of the XElement property. If you don't override this property in your derived class, this object will throw an exception, reminding you that you MUST provide an override in the derived class. The reason I did this is because properties cannot be made abstract.

public abstract class AppSettingsBase
{
    protected System.Environment.SpecialFolder m_specialFolder = 
                                    Environment.SpecialFolder.CommonApplicationData;
    protected bool     m_isDefault           = true;
    protected string   m_fileName            = "";
    protected string   m_dataFilePath        = "";
    protected string   m_settingsFileComment = "";
    protected string   m_settingsKeyName     = "";
    protected XElement m_defaultSettings     = null;

    //--------------------------------------------------------------------------------
    public bool IsDefault
    {
        get { return m_isDefault; }
        set { m_isDefault = value; }
    }
    //--------------------------------------------------------------------------------
    public string FileName
    {
        get { return m_fileName; }
        set { m_fileName = value; }
    }
    //--------------------------------------------------------------------------------
    public string SettingsFileComment
    {
        get { return m_settingsFileComment; }
        set { m_settingsFileComment = value; }
    }
    //--------------------------------------------------------------------------------
    public string SettingsKeyName
    {
        get { return m_settingsKeyName; }
        set { m_settingsKeyName = value; }
    }
    //--------------------------------------------------------------------------------
    public virtual XElement XElement
    {
        get { throw new Exception("You must provide your own XElement property."); }
        set { throw new Exception("You must provide your own XElement property."); }
    }
    //--------------------------------------------------------------------------------
    public XElement DefaultSettings
    {
        get { return m_defaultSettings; }
        set { m_defaultSettings = value; }
    }

Next comes the constructor. The only item of note here is that the class determines whether or not it's being created as a "default settings" object by evaluating the defaultSettings parameter for nullness. If it is null, then this object will be used to represent the application's default settings. Otherwise, it will represent the user settings.

//--------------------------------------------------------------------------------
protected AppSettingsBase(string appFolder, string fileName, string settingsKeyName,
                       string fileComment, XElement defaultSettings)
{
    m_defaultSettings      = defaultSettings;
    m_isDefault            = (m_defaultSettings == null);
    m_fileName             = fileName;
    m_settingsKeyName      = settingsKeyName;
    m_settingsFileComment  = fileComment;
    m_dataFilePath         = CreateAppDataFolder(appFolder);
    if (!IsDefault && m_defaultSettings != null)
    {
        XElement = m_defaultSettings;
    }
}

There are several virtual functions that provide basic functionality, and they were specified as virtual in case there was some special processing needed by the programmer. Essentially, these are the methods that load and save the settings.

//--------------------------------------------------------------------------------
public virtual void Load()
{
    bool loaded = false;
    string fileName = System.IO.Path.Combine(m_dataFilePath, m_fileName);
    if (File.Exists(fileName))
    {
        try
        {
            XDocument doc = XDocument.Load(fileName);
            var settings = doc.Descendants(SettingsKeyName);
            if (settings.Count() > 0)
            {
                foreach (XElement element in settings)
                {
                    XElement = element;
                    loaded = true;
                    // just in case we have more than one, only take the first one
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    if (!loaded && !IsDefault && m_defaultSettings != null)
    {
        Reset();
    }
}


//--------------------------------------------------------------------------------
public virtual void Save()
{
    if (!IsDefault)
    {
        string fileName = System.IO.Path.Combine(m_dataFilePath, m_fileName);
        try
        {
            XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                          new XComment(SettingsFileComment));
            var root = new XElement("ROOT", XElement);
            doc.Add(root);
            doc.Save(fileName);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}


//--------------------------------------------------------------------------------
public virtual void SetAsDefaults(ref XElement element)
{
    if (!IsDefault)
    {
        element = XElement;
    }
}

//--------------------------------------------------------------------------------
public virtual void Reset()
{
    if (!IsDefault && m_defaultSettings != null)
    {
        XElement = m_defaultSettings;
    }
}

Finally, we have the two utility methods. The first one is used to cast an integer value back to a possible enum ordinal. This method accepts an integer, and tries to find a matching ordinal within the defined set of ordinals. If a match is found, it is returned. Otherwise, the specified default value is returned. It amounts to a sanity check for values that are obtained form a data file or other external source.

//--------------------------------------------------------------------------------
public static T IntToEnum<T>(int value, T defaultValue)
{
    T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
    return enumValue;
}

The last utility method is used to create the required application data folder. By default, this is set to the special folder CommonApplicationData, but there is a property available that exposes the setting to external objects. If the specified folder exists, or if it is successfully created, the complete folder name is returned to the calling method.

    //--------------------------------------------------------------------------------
    protected string CreateAppDataFolder(string folderName)
    {
        string appDataPath = "";
        string dataFilePath = "";

        folderName = folderName.Trim();
        if (folderName != "")
        {
            try
            {
                // Set the directory where the file will come from.  The folder name 
                // returned will be different between XP and Vista. Under XP, the default 
                // folder name is "C:\Documents and Settings\All Users\Application Data\[folderName]"
                // while under Vista, the folder name is "C:\Program Data\[folderName]".
                appDataPath = System.Environment.GetFolderPath(SpecialFolder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
			// make sure w're only going to create a top-level folder
            if (folderName.Contains("\\"))
            {
                string[] path = folderName.Split('\\');
                int folderCount = 0;
                int folderIndex = -1;
                for (int i = 0; i < path.Length; i++)
                {
                    string folder = path[i];
                    if (folder != "")
                    {
                        if (folderIndex == -1)
                        {
                            folderIndex = i;
                        }
                        folderCount++;
                    }
                }
                if (folderCount != 1)
                {
                    throw new Exception("Invalid folder name specified (this function" +
                                        "only creates the root app data folder for the" + 
                                        " application).");
                }
                folderName = path[folderIndex];
            }
        }
        if (folderName == "")
        {
            throw new Exception("Processed folder name resulted in an empty string.");
        }
        try
        {
            dataFilePath = System.IO.Path.Combine(appDataPath, folderName);
            if (!Directory.Exists(dataFilePath))
            {
                Directory.CreateDirectory(dataFilePath);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return dataFilePath;
    }

}

The AppSettings Class

Since all of the heavy lifting is done in the AppSettingsBAse class, there's not really much to do here other than add our data members and properties.

public class AppSettings : AppSettingsBase
{
    // used to seed the base constructor
    private	const string APP_DATA_FOLDER	= "SharedSettingsDemo";
    private const string APP_DATA_FILENAME	= "Settings.xml";
    private const string FILE_COMMENT		= "Demo Shared User Seettings";

The constants defined above are defined there because this class is the upper level of the hierarchy chain, and the specified values will never change for this particular class (of course, they WILL change for other derived versions of this class, but for our demo app, this is perfectly fine.

    // actual data
    protected string	m_name		= "john";
    protected int		m_age		= 52;
    protected bool		m_old		= true;

    //--------------------------------------------------------------------------------
    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }
    //--------------------------------------------------------------------------------
    public int Age
    {
        get { return m_age; }
        set { m_age = value; }
    }
    //--------------------------------------------------------------------------------
    public bool Old
    {
        get { return m_old; }
        set { m_old = value; }
    }
    //--------------------------------------------------------------------------------
    public override XElement XElement
    {
        get
        {
            return new XElement(m_settingsKeyName,
                                new XElement("Name", Name),
                                new XElement("Age",  Age.ToString()),
                                new XElement("Old",  Old.ToString()));
        }
        set
        {
            Name = value.Element("Name").Value;
            Age  = Convert.ToInt32(value.Element("Age").Value);
            Old  = Convert.ToBoolean(value.Element("Old").Value);
        }
    }


    //--------------------------------------------------------------------------------
    public AppSettings(string settingsKeyName, XElement element)
            : base(APP_DATA_FOLDER, APP_DATA_FILENAME, settingsKeyName, FILE_COMMENT, element)
    {
    }

    //--------------------------------------------------------------------------------
    public AppSettings(string appDataFolder, string appDataFilename, string settingsKeyName, 
                       string fileComment, XElement element)
            : base(appDataFolder, appDataFilename, settingsKeyName, fileComment, element)
    {
    }

}

Remember, we have to override the XElement property so we have something to hand off to the load/save functions. I also provided an overloaded constructor so I can manually specify the items that are handled by the constants defined at the top of the class if the need arises.

The DemoSettingsManager Class

I love compartmentalizing things. It wraps up a lot of the ugly code into a nice neat package and centralizes access to internally held components. This class is simply a wrapper around the user settings object and the default settings object.

public class DemoSettingsManager
{
    private AppSettings m_userSettings = null;
    private AppSettings m_defaultSettings = null;

    //--------------------------------------------------------------------------------
    public AppSettings UserSettings
    {
        get { return m_userSettings; }
    }
    //--------------------------------------------------------------------------------
    public AppSettings DefaultSettings
    {
        get { return m_defaultSettings; }
    }

    //--------------------------------------------------------------------------------
    public DemoSettingsManager()
    {
        // create the default settings first
        m_defaultSettings = new AppSettings("DEMO_DEFAULT_SETTINGS", null);
        m_defaultSettings.Load();
        // so we can pass them to the user settings for initial setup
        m_userSettings = new AppSettings("DEMO_USER_SETTINGS", m_defaultSettings.XElement);
        m_userSettings.Load();
    }
}

I could certainly have added the handling of the default settings to this class, buut I was only eager to verify that the AppSettings classes would work as intended. I therefore leave this as an exercise for the programmer.

The Sample Application Solution

To test the code, the solution contains two identical applications. If you run them both at the same time, you'll see how the two applications pull data from the same data file.

History

  • 12/18/2008: Changed the code/article to reflect a comment about making the base class constructor protected instead of making the whole class abstract (with abstract methods). Thanks Navaneeth!
  • 12/17/2008: Original article posted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralFinding the user.config file of another application Pin
Kaare Tragethon26-Jun-10 18:17
Kaare Tragethon26-Jun-10 18:17 
GeneralRe: Finding the user.config file of another application Pin
#realJSOP27-Jun-10 6:33
mve#realJSOP27-Jun-10 6:33 
GeneralRe: Finding the user.config file of another application Pin
Kaare Tragethon27-Jun-10 9:11
Kaare Tragethon27-Jun-10 9:11 
GeneralRe: Finding the user.config file of another application Pin
#realJSOP27-Jun-10 23:52
mve#realJSOP27-Jun-10 23:52 
GeneralRe: Finding the user.config file of another application Pin
Kaare Tragethon28-Jun-10 9:11
Kaare Tragethon28-Jun-10 9:11 
GeneralRe: Finding the user.config file of another application Pin
#realJSOP28-Jun-10 9:36
mve#realJSOP28-Jun-10 9:36 
GeneralRe: Finding the user.config file of another application Pin
Kaare Tragethon28-Jun-10 9:48
Kaare Tragethon28-Jun-10 9:48 
GeneralRe: Finding the user.config file of another application Pin
#realJSOP28-Jun-10 15:25
mve#realJSOP28-Jun-10 15:25 
GeneralRe: Finding the user.config file of another application Pin
Kaare Tragethon28-Jun-10 16:48
Kaare Tragethon28-Jun-10 16:48 
GeneralRe: Finding the user.config file of another application Pin
#realJSOP29-Jun-10 0:09
mve#realJSOP29-Jun-10 0:09 
GeneralRe: Finding the user.config file of another application Pin
Kaare Tragethon30-Jun-10 5:08
Kaare Tragethon30-Jun-10 5:08 
QuestionCan't make property abstract? Pin
Pete Appleton22-Dec-08 16:26
Pete Appleton22-Dec-08 16:26 
AnswerRe: Can't make property abstract? Pin
#realJSOP22-Dec-08 23:16
mve#realJSOP22-Dec-08 23:16 
GeneralRe: Can't make property abstract? Pin
Pete Appleton22-Dec-08 23:44
Pete Appleton22-Dec-08 23:44 
GeneralAbout FxCop Usage Pin
#realJSOP19-Dec-08 5:53
mve#realJSOP19-Dec-08 5:53 
GeneralSome points on your class design Pin
N a v a n e e t h17-Dec-08 20:57
N a v a n e e t h17-Dec-08 20:57 
GeneralRe: Some points on your class design Pin
#realJSOP17-Dec-08 23:09
mve#realJSOP17-Dec-08 23:09 
GeneralIgolfyoo - Golf Clubs - made price - free shipping Pin
chiangkinghu18-Dec-08 15:47
chiangkinghu18-Dec-08 15:47 
GeneralRe: Some points on your class design Pin
#realJSOP18-Dec-08 2:22
mve#realJSOP18-Dec-08 2:22 
RantRe: Some points on your class design Pin
Soundman32.218-Dec-08 4:35
Soundman32.218-Dec-08 4:35 
GeneralRe: Some points on your class design Pin
Izzet Kerem Kusmezer19-Dec-08 0:27
Izzet Kerem Kusmezer19-Dec-08 0:27 
GeneralRe: Some points on your class design Pin
#realJSOP19-Dec-08 1:01
mve#realJSOP19-Dec-08 1:01 
GeneralRe: Some points on your class design Pin
Izzet Kerem Kusmezer19-Dec-08 1:15
Izzet Kerem Kusmezer19-Dec-08 1:15 
GeneralRe: Some points on your class design [modified] Pin
#realJSOP19-Dec-08 2:34
mve#realJSOP19-Dec-08 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.