从项目参考中读取appconfig

时间:2013-04-16 08:16:53

标签: c# configuration app-config

在我的主项目(“Demo”)上,我添加了一个项目引用到某个项目(“Common”)。 在“Common”项目中,我有app.config文件,其中包含:

<configuration>
    <appSettings>
        <add key="test" value="123"/>
    </appSettings>
</configuration>

和一个班级:

public class Class1
{
    public string Get()
    {
        return ConfigurationManager.AppSettings["test"];
    }
}

在我的主程序(“demo”)上我添加了这段代码:

class Program
{
    static void Main(string[] args)
    {
        Class1 c = new Class1();
        Console.WriteLine(c.Get());
        Console.ReadLine();
    }
}

由于app.config在顶层配置模型上运行,因此无效。

我记得以前我在demo项目中添加了一个带有configsection节点的app.config文件。但是我现在有问题要做 - 忘了怎么做。

有人可以告诉我demo的app.config应该是什么样的吗?

谢谢!!!

1 个答案:

答案 0 :(得分:0)

总是正在从man可执行项目中读取App.Config和Web.Config。即,您需要将Common \ app.config添加/合并到主\ app.config