Search code examples
c#configurationmanager

C# App.config on a per-user basis


I'm writing an application that has some application level configurations, but also has some configurations that should be done on a per-user basis.

I'm writing a config section for BrowserStack to use the App.config file (per msdn) which has let me define the important things that need to be stored, however, there are things that belong in a config file that don't belong in version control. In this particular instance browserstack.user and browserstack.key (which are retrieved from their documentation) belong in a separate config file and they shouldn't be checked in.

Does c# configuration allow for this kind of behavior, or would I have to modify my config section to do this? (Included below for reference)

public class BrowserStackSettings : ConfigurationSection
{
    [ConfigurationProperty("hubUrl", DefaultValue = "http://hub-cloud.browserstack.com/wd/hub/", IsRequired = false)]
    public string HubUrl
    {
        get
        {
            return (string)this["hubUrl"];
        }
        set
        {
            this["hubUrl"] = value;
        }
    }

    [ConfigurationProperty("user", IsRequired = true)]
    public string User
    {
        get
        {
            return (string)this["user"];
        }
        set
        {
            this["user"] = value;
        }
    }

    [ConfigurationProperty("key", IsRequired = true)]
    public string Key
    {
        get
        {
            return (string)this["key"];
        }
        set
        {
            this["key"] = value;
        }
    }            
}

Solution

  • C# has something called settings. The scope of one setting can be "user" or "application". Applications settings cannot be changed easily. They are stored in app's .config file in Program Files. But user settings can be changed, and of course can be different for each user. Those settings are stored in another .config file somewhere in user's profile.

    So, app should somehow allow users to change those settings, and to save them just call Properties.Settings.Default.Save();