Click here to Skip to main content
11,411,840 members (56,203 online)
Click here to Skip to main content

.NET settings files in class library projects

, 26 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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. // 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 Smile | :) .

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


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

Questionhow to do it with vb project Pinmembergunawanyuwono14-Aug-14 0:24 
AnswerRe: how to do it with vb project PinmemberJecho Jekov15-Aug-14 11:57 
GeneralMy vote of 5 Pinmemberdexterama13-Aug-14 5:33 
Question.Net settings files Pinmemberpeterkeith29-Sep-13 21:56 
AnswerRe: .Net settings files PinmemberJecho Jekov29-Sep-13 22:02 
Questionsave settings on runtime Pinmemberroee_k23-Jul-13 4:56 
AnswerRe: save settings on runtime PinmemberJecho Jekov24-Jul-13 10:01 
QuestionWithout copy/paste PinmemberWarchoon5-Dec-12 23:50 
GeneralGreat, this is exactly what I was looking for [modified] PinmemberPinx31-Aug-11 0:12 
QuestionUser-scope settings Pinmemberspringy7626-Aug-11 7:18 
AnswerRe: User-scope settings PinmemberJecho 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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 26 Aug 2011
Article Copyright 2011 by Jecho Jekov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid