Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#

.NET settings files in class library projects

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
26 Aug 2011CPOL2 min read 88.6K   2.4K   28   17
How to use .NET settings files in class library projects.

Introduction

If you ever wanted to use a .NET settings file (e.g., Settings.settings) in your library or put settings in an external configuration file (e.g., ExternalSettings.config), then this article is for you.

I've been using library assembly configuration files (e.g., MyLib.dll.config) for a while and I must say they have their purpose (despite what Microsoft may tell you).

Occasionally, I also need to use constants in my libraries and while they might never change, I feel more comfortable when putting them in a configuration file. Using a .NET settings file is the most convenient way to do that in my opinion.

Using the code

SettingsExample.PNG

Library configuration file

Using the code is very simple. First add a reference to AssemblySettings.dll and then follow these steps:

  1. Add a settings file to your library project (you may already have a default settings file "Properties\Settings.settings").
  2. Add a partial class with the same name as your settings file. For example, if your file is named "Settings.settings" then add a class named Settings in the file Settings.cs.
  3. Add the following constructor to the partial class:
  4. C#
    // Replace "Settings" with the name of your settings file.
    partial class Settings
    {
        Settings() : base(new ConfigurationFileApplicationSettings(
            Assembly.GetExecutingAssembly(), 
            typeof(Settings)
            )) { }
    }

Voilà! Your settings will be loaded from the configuration file of the library assembly.

Caveats

When you compile your library in Visual Studio, your "App.config" file will be copied to the output directory and named after the name of your library (e.g., MyLib.dll.config). However, if you add a reference to your library from another project, the ".config" file will not be copied to that project's output directory. You must do that manually or by using some deployment technique.

One way to do this automatically from VS is to create a copy of the "App.config" file in your project and name it "MyLib.dll.config". Then edit the file's properties and set "Build Action" to "Content" and "Copy to Output Directory" to "Copy if newer". Since the file is a content file, it will follow your library wherever it goes :-).

External configuration file

If you want to keep your settings in an external configuration file in a class library or a .exe project, you can do that as well. The only difference is that you must place the name of your ".config" file instead of Assembly.GetExecutingAssembly() in the above code.

C#
// Replace "Settings" with the name of your settings file.
partial class Settings
{
    Settings() : base(new ConfigurationFileApplicationSettings(
        "ExternalSettings.config", 
        typeof(Settings)
        )) { }
}

Final thoughts

Using class library configuration files could be very convenient in some cases. Being unable to do so out of the box is another example of Microsoft thinking on the behalf of developers.

To "Microsoft": give us tools. We known how to handle them ;-).

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)
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError when path has "special" characters in the name Pin
MrMikeJJ29-Jul-21 1:05
MrMikeJJ29-Jul-21 1:05 
AnswerRe: Error when path has "special" characters in the name Pin
Jecho Jekov29-Jul-21 1:18
Jecho Jekov29-Jul-21 1:18 
QuestionI followed the steps completely and it doesn't work Pin
Member 1255443430-May-16 3:44
Member 1255443430-May-16 3:44 
AnswerRe: I followed the steps completely and it doesn't work Pin
Jecho Jekov3-Dec-16 9:10
Jecho Jekov3-Dec-16 9:10 
QuestionWhat is ConfigurationFileApplicationSettings Pin
Member 96359074-Nov-15 21:53
Member 96359074-Nov-15 21:53 
AnswerRe: What is ConfigurationFileApplicationSettings Pin
Jecho Jekov3-Dec-16 9:06
Jecho Jekov3-Dec-16 9:06 
Questionhow to do it with vb project Pin
gunawanyuwono13-Aug-14 23:24
gunawanyuwono13-Aug-14 23:24 
AnswerRe: how to do it with vb project Pin
Jecho Jekov15-Aug-14 10:57
Jecho Jekov15-Aug-14 10:57 
GeneralMy vote of 5 Pin
dexterama13-Aug-14 4:33
professionaldexterama13-Aug-14 4:33 
Question.Net settings files Pin
peterkeith29-Sep-13 20:56
peterkeith29-Sep-13 20:56 
AnswerRe: .Net settings files Pin
Jecho Jekov29-Sep-13 21:02
Jecho Jekov29-Sep-13 21:02 
Questionsave settings on runtime Pin
roee_k23-Jul-13 3:56
roee_k23-Jul-13 3:56 
AnswerRe: save settings on runtime Pin
Jecho Jekov24-Jul-13 9:01
Jecho Jekov24-Jul-13 9:01 
QuestionWithout copy/paste Pin
VassilSanych5-Dec-12 22:50
VassilSanych5-Dec-12 22:50 
GeneralGreat, this is exactly what I was looking for [modified] Pin
pinx30-Aug-11 23:12
pinx30-Aug-11 23:12 
QuestionUser-scope settings Pin
springy7626-Aug-11 6:18
springy7626-Aug-11 6:18 
Ever tried to use User-scope settings (instead of application-scope settings) and then try to call Settings.Default.Upgrade() on a new app version?
While the DLL user-settings indeed are stored along with each app which uses the DLL the Upgrade() does do nothing and looses all settings.
AnswerRe: User-scope settings Pin
Jecho Jekov26-Aug-11 6:52
Jecho Jekov26-Aug-11 6:52 

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.