.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Can't get keys from App.config file in Class Library project - am I missing something?
 

Can't get keys from App.config file in Class Library project - am I missing something?

Hi there,

I'm having a bit of trouble in a C# Class Library that I'm writing - when I try to get keys from the App.config file I'm getting nothing back. I copied the code from another project of mine (a Windows Service), which works fine there. Don't understand why nothing is being returned.

What I did was:

Added an Application Configuration File to my Class Library project, named the default App.config

It looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSettings>
<add key ="CopyrightFolder" value ="Copyright\\" />
<add key ="CopyrightTagWord" value ="CopyrightTag.docx" />
</appSettings>

</configuration>


Used this code to try to read the key:

NameValueCollection appSettings = ConfigurationManager.AppSettings;
CopyrightFolder = appSettings.Get("CopyrightFolder");


At the top of the class I'm using the System.Collections.Specialized for the NameValueCollection, the class compiles fine. I have a Windows Forms app just to test the class library, when I run the function that contains the code to read the key and step through, the CopyrightFolder remains as null (it's defined as string earlier in the code).

As I say - this code is the exact same as the other project - I'm a bit baffled as to why it's working in 1 and not the other?

Thanks

Neal Hudson
You say you added this to your class library project. You need to add this to the App.config of the executable assembly. The App.config in a class library won't be read.
Tom Frey
You say you added this to your class library project. You need to add this to the App.config of the executable assembly. The App.config in a class library won't be read.
Tom Frey

So, how does one read an assembly configuration file?

Manually? Isn't there a way to get it read in automagically?

/Tommy

TommySson

You can use google to search for other answers

Custom Search

More Threads

• Get line number at unhandled exception during runtime (no debugger)
• Unexpected characters in OU name after creation
• Active window
• RS232 Serial Port programming in Visual Studio .NET 2003
• real type of a System.__ComObject
• Out Of Memory Reasons?
• windowsidentity.getcurrent().name Leaves Handles
• How to generate *.chm from *.xml in .NET 2.0?
• WebBrowser Control - html on the fly?
• Trace Listeners