I'm creating a Class Library that will be used by multiple projects, I want
to read a config file (app.config) for my database connection string(s), how
can i get the class library to read the app.config file for my connection
string?
Michael Nemtsev - 15 May 2006 20:03 GMT
Hello CSharpguy,
Just using System.Xml namespace
C> I'm creating a Class Library that will be used by multiple projects,
C> I want to read a config file (app.config) for my database connection
C> string(s), how can i get the class library to read the app.config
C> file for my connection string?
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
CSharpguy - 15 May 2006 22:19 GMT
I did, and the connection string is coming back blank from my app.config file.
its not reading the file for the database connection
> Hello CSharpguy,
>
[quoted text clipped - 11 lines]
> "At times one remains faithful to a cause only because its opponents do not
> cease to be insipid." (c) Friedrich Nietzsche
Jay B. Harlow [MVP - Outlook] - 16 May 2006 13:05 GMT
CSharpguy,
You read the app.config in a class library the exact same way you would in
an application:
using System.Configuration;
// .Net 1.x
String connectionString =
ConfigurationSettings.AppSettings["MyExcelFileName"];
// .Net 2.0
String connectionString =
ConfigurationManager.ConnectionStrings["MyExcelFileName"].ConnectionString;
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="" value=""/>
</appSettings>
<connectionStrings>
<add name="" connectionString="" providerName="" />
</connectionStrings>
</configuration>
In the .Net 2.0 case be certain to add a reference to the
System.Configuration assembly.
As you may have noticed above .Net 2.0 includes a new connectionStrings
section in the app.config for storing connection strings. I recommend using
this new section as it leverages the new DbProviderFactory methods.
The trick to reading from the app.config is, that a class library doesn't
have its own app.config, it uses the one from the application (the exe). So
you need to be certain the app.config for the executable contains your
settings!

Signature
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
| I'm creating a Class Library that will be used by multiple projects, I want
| to read a config file (app.config) for my database connection string(s), how
| can i get the class library to read the app.config file for my connection
| string?