Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


C# - using .config files with DLLs used by a web app

Posted on 2005-03-17
Medium Priority
Last Modified: 2013-11-18
Hey, I have a solution that includes multiple class library projects, and a web project.  

One of my class library projects contains database connections, so I wanted to use a .config file to define the user, database, etc.  But i'm not so sure how to do this with a DLL.  

I basically want something like a .ini file in VB6, so I don't have to include it in the project, and I can have a different file for each server, and the solution doesn't need to be rebuilt if the file is changed.

I tried adding a .config through the IDE (VS.NET 2003), but it doesn't work when I run a sample ASPX page that uses the DLL.  I also tried adding <myAppName>.config and <myAppName>.dll.config to the folder that contains the DLL.

thanks in advance.
Question by:Sketchy

Accepted Solution

shankar_ms earned 136 total points
ID: 13570508
You cannot use .config file directly with a DLL. You have to use config file with an exe and pass the dynamic parameters to the dll.

The following phrase is from the msdn
"Projects that compile a DLL cannot use dynamic properties directly, however DLL properties can be set dynamically by the .exe referencing the .dll"


Assisted Solution

imperial_p79 earned 132 total points
ID: 13598617
This is one clean way of doing it.

Have a web.config directly under the solution. At the solution, right click and add new item and add a dev.config (similarly for prod.config etc). In your solution, this will appear in a separate folder called "Solution Items". Define all your db connection, users etc in this config file.

Now in the project which you think should need this config keys, go to the project properties (right click on the project, properties) and under "COmmon Properties" go to "Build events". Under pre-build Event command line enter the following command.

XCOPY /y/i $(SolutionDir)Config\*.config $(ProjectDir)

IF EXIST  $(ProjectDir)web.config DEL $(ProjectDir)web.config
rename $(ProjectDir)dev.config web.config

whenever the build on the project happens, the correct version of the web.config will be added to it. This way we dont have to have duplicate web.config in all the projects.

Let me know if this works.

Assisted Solution

peatre earned 132 total points
ID: 13612524
As said above, only the application can access .config variables. Though just because of this does not mean you can use them. You could get the application to load the variable in and then either expose it or pass it as an arg to the called dll.

Example .config file:
<?xml version="1.0" encoding="utf-8" ?>
        <add key="DBSource" value="ukdev304" />
        <add key="DBUser" value="new_stax" />
        <add key="DBPassword" value="" />

Then use the following code to access the variables in the .config file, make sure you have 'using System.Configuration;' in your using section of your .cs file.

// Read in the settings found in the application .config file.
private void GetConfigurationSettings()
    m_DBSource = Convert.ToString
    m_DBUser = Convert.ToString
    m_DBPassword = Convert.ToString

Now that you have read the values in you can expose or pass them within your app in whichever way you wish.

Hope this helps!

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question