C# 在 Visual Studio 中组合多个配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/463733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:25:42  来源:igfitidea点击:

Combining multiple config files in Visual Studio

c#visual-studio-2008configurationapp-configconfiguration-files

提问by

I have a solution in Visual Studio 2008 which contains multiple C# projects. Each project has it's own config file (App.config and Settings.settings). This makes sense architecturally as each module is autonomous and is used in a number of different solutions.

我在 Visual Studio 2008 中有一个解决方案,其中包含多个 C# 项目。每个项目都有自己的配置文件(App.config 和 Settings.settings)。这在架构上是有意义的,因为每个模块都是自治的,并用于许多不同的解决方案。

My problem is that when I compile the solution only the config file for the startup project (or project containing the executable) is included. For other modules the config settings are compiled into the DLL. So my question is, is there any way in Visual Studio of combining multiple config files, or linking them so that settings for DLLs can be changed at runtime?

我的问题是,当我编译解决方案时,只包含启动项目(或包含可执行文件的项目)的配置文件。对于其他模块,配置设置被编译到 DLL 中。所以我的问题是,在 Visual Studio 中是否有任何方法可以组合多个配置文件或链接它们以便可以在运行时更改 DLL 的设置?

回答by Spencer Ruport

I don't believe there is. But just in case you're unaware.

我不相信有。但以防万一你不知道

回答by Mark Brittingham

I'm not sure if this is what you are looking for or if it will help but just as a lead into an area to explore, note that you can link two Config files. For example, I have my connection string in a separate file so my connectionStrings line in Web.config reads:

我不确定这是否是您正在寻找的内容,或者它是否会有所帮助,但作为进入探索领域的引导,请注意,您可以链接两个配置文件。例如,我在一个单独的文件中有我的连接字符串,所以我在 Web.config 中的 connectionStrings 行读取:

<connectionStrings configSource="WebCS.config"/>

Then, in the separate file, I have:

然后,在单独的文件中,我有:

<connectionStrings>
    <add name="BSDIConnString" 
         connectionString="Data Source=XXX;Initial Catalog=XXX;User ID=XXX;Password=XXX" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

回答by Mark Brittingham

After more digging, I found this quite useful post

经过更多的挖掘,我发现这个非常有用的帖子

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2710647c-6414-42c4-90b7-fd7603f55ae0/

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2710647c-6414-42c4-90b7-fd7603f55ae0/

Just in case anyone runs into the same issue, the above seems to be the most practical way of getting around it. Although beware that with this solution creating setup packages may become tedious:

以防万一有人遇到同样的问题,以上似乎是解决它的最实用方法。但请注意,使用此解决方案创建安装包可能会变得乏味:

http://bytes.com/groups/net-c/498720-app-config-dll-not-getting-added-setup-project

http://bytes.com/groups/net-c/498720-app-config-dll-not-getting- added-setup-project

回答by Mark Brittingham

OK good people, all you will ever need to know about VS config files:

好的人,关于 VS 配置文件,您将需要了解的所有信息:

http://geekswithblogs.net/akraus1/articles/64871.aspx

http://geekswithblogs.net/akraus1/articles/64871.aspx