.NET 配置文件 configSource 在应用程序目录文件夹之外 [英] .NET Config Files configSource outside the application directory folder

查看:25
本文介绍了.NET 配置文件 configSource 在应用程序目录文件夹之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序,一个是控制台应用程序,另一个是 ASP.NET 应用程序.他们都需要知道相同的 appSettings 和 connectionStrings.所以理想情况下,我想使用 app.config/web.config 文件的 configSource 属性将其指向一个中心位置.例如

I have two applications one a console application and the other an ASP.NET app. They both need to know the same appSettings and connectionStrings. So ideally I would like to use the configSource property of app.config/web.config files to point that to a central location. For example

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

但是失败并出现错误:

configSource 属性无效.:configSource 'D:appSettings.config' 无效.它必须引用与配置文件位于同一目录或子目录中的文件.

The configSource attribute is invalid.: The configSource 'D:appSettings.config' is invalid. It must refer to a file in the same directory or in a subdirectory as the configuration file.

无论如何仍然使用配置管理器 appSettings/connectionStrings 并从外部位置获取值?
我很高兴必须添加代码来完成这项工作,但我不想更换整个配置管理器系统.

Is there anyway to still use the configuration managers appSettings/connectionStrings and get the values from an external location?
I'm happy with having to add code to do it, but I don't want to have to replace the entire configuration manager system.

推荐答案

另一种解决方案是将配置文件作为链接添加到所有项目中,而不是实际将文件复制到项目中.然后将文件的构建操作"设置为内容"并将复制到输出目录"设置为如果更新则复制",当您编译项目时,您将在输出目录中拥有该文件.

Another solution is simply to add the configuration file in all your projects as a link instead of actually copying the file to your projects. Then set the "Build Action" of the file to "Content" and "Copy to Output Directory" to "Copy if newer" and when you compile the project you will have the file in the output directory.

要将文件作为链接添加到添加现有项目"对话框中,有一个带有下拉菜单的添加按钮.从添加"按钮的下拉菜单中选择添加为链接"以完成该过程.

To add the file as a link in the "Add Existing Item" dialog box, there is an Add button with a dropdown. Choose "Add as link" from the dropdown on the Add button to complete the process.

这篇关于.NET 配置文件 configSource 在应用程序目录文件夹之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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