共享配置文件,网页和窗口应用程序(最佳方式) [英] Share Config file for Web and Window App (Best Way)

查看:160
本文介绍了共享配置文件,网页和窗口应用程序(最佳方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个类库(核心处理部件)使用Visual Studio 2008的C#,并添加在网站上的参考。网站成功接入类库。 WEB.CONFIG具有一些配置值,其用于通过所述类库。

现在,我想访问同一个组件和配置在窗口应用程序(C#VS-2K8)。我能够访问相同的类库窗口申请。

不过,我怎么分享网站的Web.config 文件的窗口应用程序?所以说,我没有复制相同的配置。

感谢。

更新#1

在问题1的进一步的细节:我想在类库添加具有依赖于应用程序的配置文件中的配置文件来代替。例如。 Web.config文件的网站或app.config中的Windows应用程序。

这是为了减少duplicacy /在多个应用程序相同的配置冲突。

更新#2

我使用下面的code获得来自窗口和Web应用程序的外部位置的配置。但它不是为我工作。

D:\ test.exe.config 文件

 <结构>
  <的appSettings>
    <添加键=KeyName是值=键值/>
  < /的appSettings>
< /结构>
 

code背后:

 配置配置= ConfigurationManager.OpenExeConfiguration(D:\\ test.exe.config);
            字符串将strValue = config.AppSettings.Settings [KeyName是]值。
 

解决方案

是的,我得到了解决。 由于鲍勃·霍恩 JoeBilly 的宝贵意见。

我已经实现了配置按我的要求。我寻觅了很多了完整的解决方案,但总是得到的概念和小块code,这就是为什么我提供了完整的工作code。

通过下述$ C $的帮助c可以实现以下和配置玩你想要的。

  1. 常见配置 [CommonDB] 可以被定义为一个单一的 存储库,并且可以存储在外部XML文件中。类库将越来越为网络总是一个值,以及Windows应用程序。
  2. 网站的具体配置 [WebDBConn] 可以存储在web.config中。
  3. 窗口应用程序特定的配置 [WindowDBConn] 可提及的app.config。
  4. 如果您要使用相同的密钥不同的值 [INPUT_PATH] 为每个应用程序,然后使用相同的密钥和差异值wen.config和App.config中。但请记住,关键不应该在common.config其他明智的值,它是在common.config可将有所回升可用。
  5. 的好处是,您不必恰克背后的方法您code 获取这些值。

Common.Config 的类库/常见配置。

 < XML版本=1.0编码=UTF-8&GT?;
<的appSettings>
    <添加键=CommonDB值=CommonDBValue/>
< /的appSettings>
 

Web.config中的网站

 <结构>
    <的appSettings文件=D:\ Common.config>
        <添加键=WebDBConn值=WebDBConnValue/>
        <添加键=INPUT_PATH值=INPUT_PATH_WEB/>
    < /的appSettings>
< /结构>
 

的App.config 的Windows应用程序

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
    <的appSettings文件=D:\ Common.config>
        <添加键=WindowDBConn值=WindowDBConnValue/>
        <添加键=INPUT_PATH值=INPUT_PATH_WINDOW/>
    < /的appSettings>
< /结构>
 

$ C $后面

ç

 字符串configValue = ConfigurationSettings.AppSettings [CommonDB];
 

谢谢...

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;

解决方案

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...

这篇关于共享配置文件,网页和窗口应用程序(最佳方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆