49

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

如何访问配置?

ConfigurationManager.AppSettings["config1"]
4

7 回答 7

61

Let's say you have this folder structure:

  • Solution
    • Project1
    • Project2
    • Project3

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 link from the drop down box next to the Add button.

      Add as link

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.

于 2009-09-01T11:29:20.587 回答
20

通用配置文件

<?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>

访问映射的配置文件

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string value = configuration.AppSettings.Settings["key1"].Value;
于 2009-09-02T10:07:03.697 回答
8

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

添加现有项目对话框

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

马克

于 2009-09-01T12:20:48.653 回答
8

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

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

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

如果您创建自己的自定义配置部分,则可以将这些部分“外包”给可以共享的外部文件。想象一下,您创建了自己的名为“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>

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

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

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

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

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

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

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

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

马克

于 2009-09-01T16:37:43.427 回答
6

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

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

于 2009-09-01T11:40:50.423 回答
5

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

VS2015 C# 共享项目

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

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

Visual Studio 2015 - 共享项目教程:

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

添加共享项目

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

应用程序配置

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

添加参考

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

我意识到这是一个较老的问题,但自从谷歌出现了这个问题,我想我会回答这个问题,所以当其他人正在寻找同样的东西时,他们知道这个非常强大的 VS 功能。

于 2017-02-10T15:47:08.230 回答
1
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);
}
于 2014-12-20T07:46:27.423 回答