Saving Application Settings In .NET

I had a requirement to allow certain users to change some application settings, but also to ensure those settings applied to all users. Initially my application used built in .NET settings set to Application scope. The problem these are read-only at runtime. I wanted a quick and dirty way to save these settings and make them apply to all users. Now this is a bit of a hack and I’m sure if I investigate further I will find better ways to do this…but what I did is…

I made the settings “user” scope then took the user settings (user.config) file and placed it in same directory as Application. I added reference to System.Configuration and used the following code at application startup:

try
{
    // retrieve the filename of current user.config
    string settingsFile=System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;

    // get filename of user.config in same path as current .exe
    string sourceFile=Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),”user.config”);

   // copy the source file to settings file location
   System.IO.File.Copy(sourceFile, settingsFile, true);

   // reload settings
   Properties.Settings.Default.Reload();
}
catch (Exception ex)
{
    // something went wrong
    MessageBox.Show(
        string.Format(“Failed to load settings. Error message: {0}”,
        ex.Message),
        “App”,
        MessageBoxButtons.OK,
        MessageBoxIcon.Error);
}

 

Now when I want to save the settings, I just copy the file back (The user running tool at that time must have permission to write to user.config in the location you are saving it)

 

try
{
    // save user scope settings to user.config in user profile
    Properties.Settings.Default.Save();

    // copy saved user.config from user profile back to location of .EXE
    System.IO.File.Copy(
        System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath,Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),”user.config”), true);
}
catch (Exception ex)
{
    MessageBox.Show(
        string.Format(“Unable to save changes. Error message: {0}”, ex.Message),
        “Default”,
        MessageBoxButtons.OK,
        MessageBoxIcon.Error);
}

 

 

About chentiangemalc

specializes in end-user computing technologies. disclaimer 1) use at your own risk. test any solution in your environment. if you do not understand the impact/consequences of what you're doing please stop, and ask advice from somebody who does. 2) views are my own at the time of posting and do not necessarily represent my current view or the view of my employer and family members/relatives. 3) over the years Microsoft/Citrix/VMWare have given me a few free shirts, pens, paper notebooks/etc. despite these gifts i will try to remain unbiased.
This entry was posted in .NET and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s