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

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

问题描述

我在 Visual Studio 2008 中使用 C# 创建了一个类库(核心处理组件)并在网站中添加了引用.网站访问类库成功.Web.config 有一些配置值,供类库使用.

现在,我想在 Window 应用程序 (C# VS-2k8) 中访问相同的组件和配置.我能够在窗口应用程序中访问相同的类库.

但是,我如何与Window应用程序共享网站的Web.config文件?这样,我就不必复制相同的配置.

谢谢.

更新#1

关于问题 1 的更多细节:我想在类库中添加配置文件,而不是依赖于应用程序的配置文件.例如.网站中的 web.config 或 windows 应用程序中的 app.config.

这是为了减少多个应用中相同配置的重复/冲突.

更新#2

我正在使用以下代码从窗口和 Web 应用程序的外部位置获取配置.但它对我不起作用.

D: est.exe.config 文件

<预><代码><配置><应用设置><add key="KeyName" value="KeyValue"/></appSettings></配置>

背后的代码:

Configuration config = ConfigurationManager.OpenExeConfiguration("D:\test.exe.config");string strValue = config.AppSettings.Settings["KeyName"].Value;

解决方案

是的,我找到了解决方案.感谢 Bob HornJoeBilly 提供宝贵意见.

我已经按照我的要求实现了配置.我搜索了很多完整的解决方案,但总是得到概念和一小段代码,这就是我提供完整工作代码的原因.

借助下面提到的代码,您可以实现以下目标并根据需要进行配置.

  1. 通用配置 [CommonDB] 可以定义为单个存储库,可以存储在外部 XML 文件中.类库将始终为 Web 和 Windows 应用程序提供一个价值.
  2. 网站特定配置[WebDBConn]可以存储在web.config中.
  3. Window Application specific 配置 [WindowDBConn] 可以在 app.config 中提及.
  4. 如果您想根据应用程序使用具有不同值的相同密钥 [INPUT_PATH],请在 wen.config 和 app 中使用相同的密钥和差异值.配置.但请记住,common.config 中不应提供该键,否则将选取 common.config 中可用的值.
  5. 好消息是您无需更改背后的代码即可获取这些值.

Common.Config 用于类库/通用配置

Web.Config 用于网站

<预><代码><配置><appSettings file="D:Common.config"><add key="WebDBConn" value="WebDBConnValue"/><add key="INPUT_PATH" value="INPUT_PATH_WEB"/></appSettings></配置>

App.config Windows 应用程序

背后的代码

string 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: est.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...

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

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