visual-studio 如何共享 App.config?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/426245/
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
How to Share App.config?
提问by Yoann. B
How can I share/link App.config or Web.config between multiple projects in a visual studio solution ?
如何在 Visual Studio 解决方案中的多个项目之间共享/链接 App.config 或 Web.config?
回答by Enrico Campidoglio
In the Add Existing Itemdialog that you get from Visual Studio's Solution Explorer, you can add a link to another file on disk to the project. In order to do this, you will have to click on the down-arrow on the right side of the Addbutton and choose Add As Link.
在从 Visual Studio 的解决方案资源管理器获得的添加现有项目对话框中,您可以将磁盘上另一个文件的链接添加到项目中。为此,您必须单击“添加”按钮右侧的向下箭头,然后选择“添加为链接”。
回答by nawfal
Another approachfrom Microsoft:
微软的另一种方法:
Use the fileattribute of the
<appSettings>element to specify an external file which will define the common<appSettings>elements.The external file will have the same schema as that of an app.config file with the exception that the root node must be
<appSettings>rather than<configuration>.Creating a common configuration file:
- On the File menu, point to New, then click File.
In the New File dialog box, do the following:
a. In the Categories pane, choose General.
b. In the Templates pane, choose XML File.
c. Click on the Open button to create a new common configuration settings file.
Add a new
<appSettings>element.
Add as many common configuration settings as required within the
<appSettings>node using<add>elements the same way as you would with any normal app.config file.
Save the configuration file.
Specifying the common configuration file in each project
Open the app.config file of each client project.
Navigate to the
<appSettings>element within the<configuration>node. If an element does not exist, add a new one.
Add a new attribute file to the
<appSettings>element and specify the relative path of the common configuration file as its value.
The client project will now be able to access the common configuration settings.Example
The following example shows how to define the common configuration settings in an external file.
<?xml version="1.0" encoding="utf-8" ?> <appSettings> <add key="commonSetting1" value="MyApplication1" /> <add key="commonSetting2" value="MySetting" /> </appSettings>The following example shows how to specify the path to the common configuration file in a project's app.config file.
<?xml version="1.0" encoding="utf-8" ?> <appSettings file=”c:\commonSettings.config”> <add key="myAppSpecificSetting" value="Setting1" /> </appSettings>As shown in the example, a client project's app.config file can have additional settings specified in the
<appSettings>element in addition to pointing to the common configuration file. If the same setting is specified multiple times, the last value specified is used. If the same setting is specified in both the common configuration file and the client project app.config file, the value specified in the common configuration file is used.
使用
<appSettings>元素的文件<appSettings>属性来指定将定义公共元素的外部文件。外部文件将具有与 app.config 文件相同的架构,但根节点必须是
<appSettings>而不是<configuration>.创建通用配置文件:
- 在文件菜单上,指向新建,然后单击文件。
在“新建文件”对话框中,执行以下操作:
一个。在类别窗格中,选择常规。
湾 在模板窗格中,选择 XML 文件。
C。单击“打开”按钮以创建新的通用配置设置文件。
添加一个新
<appSettings>元素。
<appSettings>使用<add>元素在节点中根据需要添加尽可能多的通用配置设置,就像使用任何普通 app.config 文件一样。
保存配置文件。
指定每个项目中的通用配置文件
打开每个客户端项目的 app.config 文件。
导航到节点
<appSettings>内的元素<configuration>。如果元素不存在,则添加一个新元素。
为
<appSettings>元素添加一个新的属性文件,并指定通用配置文件的相对路径作为其值。
客户端项目现在将能够访问通用配置设置。例子
以下示例显示如何在外部文件中定义通用配置设置。
<?xml version="1.0" encoding="utf-8" ?> <appSettings> <add key="commonSetting1" value="MyApplication1" /> <add key="commonSetting2" value="MySetting" /> </appSettings>以下示例显示如何在项目的 app.config 文件中指定公共配置文件的路径。
<?xml version="1.0" encoding="utf-8" ?> <appSettings file=”c:\commonSettings.config”> <add key="myAppSpecificSetting" value="Setting1" /> </appSettings>如示例中所示,
<appSettings>除了指向公共配置文件之外,客户端项目的 app.config 文件还可以在元素中指定其他设置。如果多次指定相同的设置,则使用最后指定的值。如果在公共配置文件和客户端项目 app.config 文件中指定了相同的设置,则使用公共配置文件中指定的值。
回答by Charles Bretana
First of all, remember that an App.config, or web.config, is visble to all code running in any project that is a used within the "process" established by the start-up project within a solution... i.e. if your solution has one console app and 5 class libraries, or one WinForms app and 4 class libraries, or one WIndows service and 3 class libraries, or one console app being used as startup project, and another console app being used as a class library, then you have no problem, the configuration app.config and all referenced files, are visible from all of the projects.
首先,请记住 App.config 或 web.config 对任何项目中运行的所有代码都是可见的,这些代码在解决方案中的启动项目建立的“过程”中使用...即,如果您的解决方案有一个控制台应用程序和 5 个类库,或者一个 WinForms 应用程序和 4 个类库,或者一个 WIndows 服务和 3 个类库,或者一个控制台应用程序用作启动项目,另一个控制台应用程序用作类库,然后你没有问题,配置 app.config 和所有引用的文件,在所有项目中都是可见的。
Secondly, if you will have two or more separate executable processes running in your solution, (like a windows service as a server, AND a winforms client), then if you want them to share specific config settings, you couldput those settings in the machine.config file, although you should be careful in doing this.. and some enterprise server teams frown on it..
其次,如果您的解决方案中有两个或多个独立的可执行进程在运行(例如作为服务器的 Windows 服务和一个 winforms 客户端),那么如果您希望它们共享特定的配置设置,您可以将这些设置放在machine.config 文件,尽管您在执行此操作时应该小心.. 并且一些企业服务器团队对此不屑一顾..
回答by Goldi
According to the Post of nawfal, use the configSource attribute, if you want to manipulate the appSettings with the ConfigurationManager:
根据 nawfal 的帖子,如果要使用 ConfigurationManager 操作 appSettings,请使用 configSource 属性:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings configSource=”c:\commonSettings.config”>
    <add key="myAppSpecificSetting" value="Setting1" />
</appSettings>

