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?
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 Nietzsche
I did, and the connection string is coming back blank from my app.config file. its not reading the file for the database connection "Michael Nemtsev" wrote: > 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 Nietzsche > > >
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! -- Hope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "CSharpguy" wrote in message news:143C9A48-2239-4E2E-B465-BB1AD601C0D9@microsoft.com... | 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? | |