0

I have created a Class Library (Core Processing Component) using C# in Visual Studio 2008 and added the reference in Website. Website accessing the class library successfully. Web.config having some configuration values, which is used by the Class Library.

Now, I want to access the same component and configuration in a Window Application (C# VS-2k8). I am able to access the same class library in window application.

But, How do i Share the Web.config file of website with the Window Application? So that, i don't have to replicate the same configuration.

Thanks.

Update# 1

Further detail on Question 1: I would like to add config file in Class Library instead of having dependent on application's config file. E.g. web.config in website or app.config in windows application.

This is to reduce the duplicacy/conflicts of same configurations in multiple apps.

Update# 2

I am using the following code to get the configuration from the external location from window and web application. But it is not working for me.

D:\test.exe.config file

<configuration>
  <appSettings>
    <add key="KeyName" value="KeyValue"/>
  </appSettings>
</configuration>

Code Behind:

Configuration config = ConfigurationManager.OpenExeConfiguration("D:\\test.exe.config");
            string strValue = config.AppSettings.Settings["KeyName"].Value;
Arun Singh
  • 1,538
  • 4
  • 19
  • 43
  • Did the new information in my answer help you? – Bob Horn Mar 23 '12 at 22:06
  • Bob your help is appreciated, but the approach is not working for me. It is picking the (Wrong) configuration of current application. I am unable to find the complete article to read the configuration. Can you help me for second question or should i asked as a separate question. – Arun Singh Mar 24 '12 at 14:01
  • If you want to add your code to your question, I'll take a look. – Bob Horn Mar 24 '12 at 16:05
  • 2
    You have **two** distinct questions, so ask **two questions** - putting both in one stackoverflow question reduces your chances of getting a good answer to both, plus it might make the answers less discoverable to others in future =) – Rob Mar 24 '12 at 16:36
  • May i know, on which basis the question is down voted? – Arun Singh Mar 25 '12 at 07:08

3 Answers3

2

That's an interesting issue. One option is to use a different persistence mechanism that's intended for multiple consumers, like a database. Can you store your config data there?

OR... you could use the answer provided here:

Accessing App.config in a location different from the binary

Community
  • 1
  • 1
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • Thanks Bob, But i am storing the connection string itself in web.config. Looking for an alternate way of having single copy of the configurations. May be, we can have a XML file and include the file in web.config for web app, as well as in app.config for window application. – Arun Singh Mar 22 '12 at 18:01
  • Well, if it's just the connection string, I think it's more work to use a centralized config than to just have it in two places. For anything beyond the connection string, a DB would work. – Bob Horn Mar 22 '12 at 18:40
  • 1
    I think I found your answer. I revised my answer. Let me know if that's what you were looking for. – Bob Horn Mar 22 '12 at 19:24
2

Note that if you need to externalize and then centralize nodes like connectionStrings and appSettings, they accept a configSource to point to a separate file.

However, not all configuration elements supports this attribute.

<appSettings configSource="appSettings.config"/>
<connectionStrings configSource="connectionStrings.config"/>

In the .NET Framework version 2.0, you can now include configuration settings in a separate file for all configuration elements that support the configSource attribute. However, when you use the configSource attribute, you must move the entire section to the separate file because there is no merging of element settings. There is a one-time write to the Web.config file when using the configSource attribute. This causes the application to restart, but subsequent updates to the section are written directly to the separate file and do not cause subsequent application restarts. For more information, see ConfigSource.

JoeBilly
  • 3,057
  • 29
  • 35
2

Yes, I got the solution. Thanks to Bob Horn and JoeBilly for valuable inputs.

I have implemented the configuration as per my requirement. I have searched a lot for the complete solution but always getting the concepts and small piece of code, that's why i am providing the complete working code.

With the help of below mentioned code you can achieve the followings and play with the configuration as you want.

  1. Common Configuration [CommonDB] can be defined as a single repository and can be stored in external XML file. Class library will be getting always one value for web as well as windows application.
  2. Website specific configuration [WebDBConn] can be stored in web.config.
  3. Window Application specific configuration [WindowDBConn] can be mentioned in app.config.
  4. If you want to use same Key with Different values [INPUT_PATH] as per the application, then use same key and the diff values in wen.config and app.config. But remember that key should not be available in the common.config other wise the value which is available in the common.config will be picked up.
  5. The good thing is that you don't have to chage your code behind approach for getting these values.

Common.Config for Class Library/Common configuration

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="CommonDB" value="CommonDBValue" />    
</appSettings>

Web.Config for Website

<configuration>
    <appSettings file="D:\Common.config">
        <add key="WebDBConn" value="WebDBConnValue" />
        <add key="INPUT_PATH" value="INPUT_PATH_WEB" />
    </appSettings>
</configuration>

App.config for Windows Application

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings file="D:\Common.config">
        <add key="WindowDBConn" value="WindowDBConnValue" />
        <add key="INPUT_PATH" value="INPUT_PATH_WINDOW" />
    </appSettings>
</configuration>

Code behind

string configValue = ConfigurationSettings.AppSettings["CommonDB"];

Thanks...

Arun Singh
  • 1,538
  • 4
  • 19
  • 43