2

我有一个解决方案和 4 个项目。

在所有项目中,我都有一个 APP.Config 文件。

我可以在同一个项目上使用它。

我有一个包含此文件 PublicApp.Config 的解决方案文件夹

我想在我的所有项目(4个项目)中使用这个配置。

add existing file将此文件归档在所有 4 个项目中Add as link

但我无法访问<appSettings>

我希望我的一个项目有一个类,将所有公共配置映射到一个属性。

我的解决方案是这样的:

我有这个解决方案文件夹Configurations,里面有这个文件名PublicAPP.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ComputerName" value="Ardalan-Pc"/>
  </appSettings>
</configuration>

GetConfiguration.cs在另一个项目中有这个文件,用于propetrie中的地图配置。

namespace ClassLibrary1
{

public static class GetConfiguration
{

    public static string ComputerName
    {
        get
        {

            return System.Configuration.ConfigurationManager.AppSettings["ComputerName"];
        }
    }

}

}

现在我不知道如何从我的 PublicAPP.Config 中获取 AppSettings["ComputerName"]。

4

1 回答 1

2

我终于意识到我可以做到以下几点。

我可以将一个项目与一个包含我所有公共配置的 App.Config 一起使用,而不是使用解决方案文件夹。

而且我认为这个项目最是我的解决方案中的启动项目。因为 System.Configuration.ConfigurationManager 只能从启动项目的 app.config 中获取属性

我在这个项目中创建了下班。

namespace ClassLibrary1
{

public static class GetConfiguration
{

public static string ComputerName
{
    get
    {

        return System.Configuration.ConfigurationManager.AppSettings["ComputerName"];
    }
}

}

}

然后我在所有其他项目的项目中引用并使用这个静态类。

当然,如果有人有更好的方法,请给我信息。

于 2013-04-14T06:05:08.417 回答