Click here to Skip to main content
15,166,066 members
Articles / Programming Languages / C#
Posted 26 Aug 2011


28 bookmarked

.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
How to use .NET settings files in class library projects.


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


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(
            )) { }

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


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.

// Replace "Settings" with the name of your settings file.
partial class Settings
    Settings() : base(new ConfigurationFileApplicationSettings(
        )) { }

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 ;-).


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


About the Author

Jecho Jekov
Software Developer (Senior)
Bulgaria Bulgaria
No Biography provided

Comments and Discussions

QuestionError when path has "special" characters in the name Pin
MrMikeJJ29-Jul-21 2:05
MemberMrMikeJJ29-Jul-21 2:05 
AnswerRe: Error when path has "special" characters in the name Pin
Jecho Jekov29-Jul-21 2:18
MemberJecho Jekov29-Jul-21 2:18 
QuestionI followed the steps completely and it doesn't work Pin
Member 1255443430-May-16 4:44
MemberMember 1255443430-May-16 4:44 
AnswerRe: I followed the steps completely and it doesn't work Pin
Jecho Jekov3-Dec-16 10:10
MemberJecho Jekov3-Dec-16 10:10 
QuestionWhat is ConfigurationFileApplicationSettings Pin
Member 96359074-Nov-15 22:53
MemberMember 96359074-Nov-15 22:53 
AnswerRe: What is ConfigurationFileApplicationSettings Pin
Jecho Jekov3-Dec-16 10:06
MemberJecho Jekov3-Dec-16 10:06 
Questionhow to do it with vb project Pin
gunawanyuwono14-Aug-14 0:24
Membergunawanyuwono14-Aug-14 0:24 
AnswerRe: how to do it with vb project Pin
Jecho Jekov15-Aug-14 11:57
MemberJecho Jekov15-Aug-14 11:57 
GeneralMy vote of 5 Pin
dexterama13-Aug-14 5:33
professionaldexterama13-Aug-14 5:33 
Question.Net settings files Pin
peterkeith29-Sep-13 21:56
Memberpeterkeith29-Sep-13 21:56 
AnswerRe: .Net settings files Pin
Jecho Jekov29-Sep-13 22:02
MemberJecho Jekov29-Sep-13 22:02 
Questionsave settings on runtime Pin
roee_k23-Jul-13 4:56
Memberroee_k23-Jul-13 4:56 
AnswerRe: save settings on runtime Pin
Jecho Jekov24-Jul-13 10:01
MemberJecho Jekov24-Jul-13 10:01 
QuestionWithout copy/paste Pin
VassilSanych5-Dec-12 23:50
MemberVassilSanych5-Dec-12 23:50 
GeneralGreat, this is exactly what I was looking for [modified] Pin
pinx31-Aug-11 0:12
Memberpinx31-Aug-11 0:12 
QuestionUser-scope settings Pin
springy7626-Aug-11 7:18
Memberspringy7626-Aug-11 7:18 
AnswerRe: User-scope settings Pin
Jecho Jekov26-Aug-11 7:52
MemberJecho Jekov26-Aug-11 7:52 
My solution was never designed to support user-level settings and although you can read them you cannot change them. I may look into this issue when I have some spare time. However, I generally prefer using XML serializable types to store user-level and even application-level settings. XML serialization makes storing and loading complex settings a snap. The only problem is (and that's not really a problem) to store configuration files at the right place. For user-level settings that could be the Environment.SpecialFolder.ApplicationData (roaming) or the Environment.SpecialFolder.LocalApplicationData (non-roaming) directory.

The initial reason for why I build the solution was to put constants out of my code so that I can modify them should a need arise.

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.