单个 app.config 多项目 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1361913/
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-06 15:39:06  来源:igfitidea点击:

single app.config multi-project c#

c#config

提问by ala

I want to use a single app.config by 3 different projects.

我想通过 3 个不同的项目使用单个 app.config。

How to access the configurations?

如何访问配置?

ConfigurationManager.AppSettings["config1"]

采纳答案by ala

The common config file

通用配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section 
            name="appSettings" 
            type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
            />
    </configSections>
    <appSettings>
        <add key="key1" value="value1"/>
    </appSettings>
</configuration>

To access mapped config file

访问映射的配置文件

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string value = configuration.AppSettings.Settings["key1"].Value;

回答by Jon Grant

Let's say you have this folder structure:

假设您有以下文件夹结构:

  • Solution
    • Project1
    • Project2
    • Project3
  • 解决方案
    • 项目1
    • 项目2
    • 项目3

Do this:

做这个:

  1. Create the App.config file in the Solution level folder. You won't find an option to add an App.config file from the templates, so just create a new empty text file with the name App.config, and paste in the contents of a regular App.config file.
  2. For each project in Solution Explorer:

    1. Right click and select Add> Existing Item
    2. Locate the file
    3. Select Add as linkfrom the drop down box next to the Addbutton.

      Add as link

  1. 在解决方案级别文件夹中创建 App.config 文件。您不会找到从模板添加 App.config 文件的选项,因此只需创建一个名为 App.config 的新空文本文件,然后粘贴常规 App.config 文件的内容。
  2. 对于解决方案资源管理器中的每个项目:

    1. 右键单击并选择Add>Existing Item
    2. 找到文件
    3. Add as linkAdd按钮旁边的下拉框中选择。

      添加为链接

Edited to add:

编辑添加:

You correctly state that the above method only shared the file up to build-time. To use a shared file at run-time, see the answers to this question.

您正确声明上述方法仅在构建时共享文件。要在运行时使用共享文件,请参阅此问题答案

回答by Greg D

One design option is to avoid accessing the app.config directly from your class library projects altogether, thus avoiding the extra external dependency.

一种设计选项是完全避免直接从您的类库项目访问 app.config,从而避免额外的外部依赖。

Rather, only your executable project knows about the config file and it can explicitly pass the appropriate config information to the libraries when it creates objects from them or initializes them.

相反,只有您的可执行项目知道配置文件,并且当它从它们创建对象或初始化它们时,它可以显式地将适当的配置信息传递给库。

回答by marc_s

Here's the "Add existing item" dialog in VS 2008:

这是 VS 2008 中的“添加现有项目”对话框:

Add existing item dialog

添加现有项目对话框

Click on the little dropdown indicator on the "Add" button and pick "Add as Link" from the context menu.

单击“添加”按钮上的小下拉指示器,然后从上下文菜单中选择“添加为链接”。

Marc

马克

回答by marc_s

I have found the button, and opened the app.config as link, however that caused when build to again create separate config file for each project, and therefore, when deploying the 3 project, i will have 3 config files. What I wanted to do, is keeping a single file for all projects in a certain solution. Can I do that?

我找到了按钮,并将 app.config 作为链接打开,但是这导致构建时再次为每个项目创建单独的配置文件,因此,在部署 3 个项目时,我将有 3 个配置文件。我想做的是为某个解决方案中的所有项目保留一个文件。我可以这样做吗?

Yes - you cando it, but shouldyou do it?

是的 - 你可以做到,但你应该这样做吗?

The basic assumption in a .NET app is that one app = one config file. Out of the box, and with an easy method, you cannot share config files between applications.

.NET 应用程序中的基本假设是一个应用程序 = 一个配置文件。开箱即用,使用简单的方法,您无法在应用程序之间共享配置文件。

If you create your own custom config sections, you could "outsource" those to external files, which could be shared. Imagine you create your own custom config section called "MyConfiguration", then your app.config would look something like that:

如果您创建自己的自定义配置部分,您可以将这些“外包”给可以共享的外部文件。想象一下,您创建了自己的名为“MyConfiguration”的自定义配置部分,那么您的 app.config 将如下所示:

<configuration>
  <configSections>
    <section name="MyConfiguration" 
             type="MyConfigurationSection, MyConfigurationAssembly" />
  </configSections>

  <MyConfiguration>
    <nestedElement>
      <dateTimeValue>10/16/2006</dateTimeValue>
      <integerValue>1</integerValue>
    </nestedElement>
  </MyConfiguration>
</configuration>

You could have your "MyConfiguration" section in its own file, and reference it from your app's config:

您可以将“MyConfiguration”部分放在自己的文件中,并从应用程序的配置中引用它:

<configuration>
  <configSections>
    <section name="MyConfiguration" 
             type="MyConfigurationSection, MyConfigurationAssembly" />
  </configSections>

  <MyConfiguration configSource="MyConfiguration.config" />
</configuration>

and your "MyConfiguration.config" would then contain:

然后您的“MyConfiguration.config”将包含:

  <MyConfiguration> 
    <nestedElement>
      <dateTimeValue>10/16/2006</dateTimeValue>
      <integerValue>1</integerValue>
    </nestedElement>
  </MyConfiguration>

By doing this, you could "externalize" and thus share at least the bulk of your config settings - provided they're in your own custom config sections.

通过这样做,您可以“外部化”并因此至少共享大部分配置设置 - 前提是它们位于您自己的自定义配置部分中。

For more info and an excellent intro to .NET 2.0 and up configuration mysteries, see Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

有关 .NET 2.0 及更高版本配置奥秘的更多信息和出色介绍,请参阅 Jon Rista 在 CodeProject 上关于 .NET 2.0 配置的三部分系列。

Highly recommended, well written and extremely helpful!

强烈推荐,写得很好,非常有帮助!

Marc

马克

回答by Kapil dev

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config = ConfigurationManager.OpenExeConfiguration(Path.Combine(@"D:\", "config.exe"));
foreach (string key in config.AppSettings.Settings.AllKeys)
{
   string value = config.AppSettings.Settings[key].Value;
   ConfigurationManager.AppSettings.Set(key, value);
}

回答by Andy Braham

I understand this is a old question but there is a much easier way of achieving this. If you are using Visual Studio 2008 or higher there is a Project type called "Shared Project".

我知道这是一个老问题,但有一种更简单的方法来实现这一点。如果您使用的是 Visual Studio 2008 或更高版本,则有一个名为“共享项目”的项目类型。

VS2015 C# Shared Project

VS2015 C# 共享项目

A Shared Project can have pretty much anything that another types of Projects can contain. This is not only for C# but for all languages that VS2015 supports. When something is included in the Shared Project it is available to other projects after you add a reference to it (see below).

共享项目几乎可以包含其他类型的项目可以包含的任何内容。这不仅适用于 C#,而且适用于 VS2015 支持的所有语言。当某些内容包含在共享项目中时,在您添加对它的引用后,它可用于其他项目(见下文)。

The major difference with Classes in a Shared Project VS a Shared Library is when you compile the program everything that is in the Shared Project will be Compiled directly into your project not as a separate file (.dll, .exe). Think of it like everything that is in the Shared Project is inserted into the other projects. Here is a small tutorial on setting this up and using it:

共享项目中的类与共享库的主要区别在于,当您编译程序时,共享项目中的所有内容都将直接编译到您的项目中,而不是作为单独的文件(.dll、.exe)。把它想象成共享项目中的所有内容都被插入到其他项目中。这是一个关于设置和使用它的小教程:

Visual Studio 2015 - Shared Project Tutorial:

Visual Studio 2015 - 共享项目教程:

Create the New Shared Project by selecting File->New->Projector Right Click on the Solution in the Solution Explorer and select Add->New Project. When the Dialog shows up select "Shared Project", give the Project a name TestShared in this example.

通过选择File->New->Project或右键单击解决方案资源管理器中的解决方案并选择Add->New Project来创建新的共享项目。当对话框出现时,选择“共享项目”,在本例中为项目命名为 TestShared。

Add Shared Project

添加共享项目

After the New Project is added you can add anything you need to be available to other projects. In this case we will add the app.config. Right Click on the Shared Project and select Add->New Item. Select Visual C#->Data->XML File naming it obviously to app.config.

添加新项目后,您可以添加其他项目所需的任何内容。在这种情况下,我们将添加 app.config。右键单击共享项目并选择Add->New Item。选择 Visual C#->Data->XML File,显然将其命名为 app.config。

app.config

应用程序配置文件

Finally add a reference to the Shared Project by Right Clicking the Project that you need to share the project with and select Add->Reference. In the Reference Manager Dialog select your Shared Project, it will be listed under the "Shared Projects" item on the Left.

最后通过右键单击您需要与之共享项目的项目并选择添加->引用来添加对共享项目的引用。在参考管理器对话框中选择您的共享项目,它将列在左侧的“共享项目”项下。

Add Reference

添加参考

Now everything that is in the Shared Project is available in the other project, there is no need to do any using imports or anything like that. It simply works. This pattern is quite handy when you are developing a program and need to have several different GUI's (Windows, iOS, Android etc). For example you could have one shared project for the "Core" functionality and then have a separate GUI Project for each of the different Operating Systems you want to support in your program.

现在共享项目中的所有内容都可以在另一个项目中使用,无需使用导入或类似的操作。它只是有效。当您开发程序并需要有几个不同的 GUI(Windows、iOS、Android 等)时,这种模式非常方便。例如,您可以为“核心”功能创建一个共享项目,然后为您希望在程序中支持的每个不同操作系统创建一个单独的 GUI 项目。

I realize that this is a older question but since this showed up in Google I thought I would answer this so when others are looking for the same thing they know about this very powerful VS feature.

我意识到这是一个较旧的问题,但由于它出现在 Google 中,我想我会回答这个问题,因此当其他人正在寻找他们对这个非常强大的 VS 功能所了解的相同内容时。