繁体   English   中英

在.NET Standard中使用配置文件不起作用

[英]Using config file in .NET Standard does not work

如何在.NET Standard项目中使用控制台配置文件(App.config)。

我在App.config中有一个控制台应用程序:

<appSettings>
    <add key="Test" value="test" />
</appSettings>

在我的.NET Standard项目中,我添加了NuGet包:

Install-Package System.Configuration.ConfigurationManager -Version 4.5.0 

并有一个类返回该键的值:

public string Test()
        {  
            return ConfigurationManager.AppSettings["Test"];
        }

这只是为了测试我是否可以在.NET Standard项目中使用App.config设置。

但我收到此错误消息:

System.IO.FileNotFoundException:'无法加载文件或程序集'System.Configuration.ConfigurationManager,Version = 4.0.1.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51'或其依赖项之一。 该系统找不到指定的文件。'

当我在Program.cs中执行此操作时:

Class class = new Class();
string result = class.Test();

我能够重现您提到的行为,并修复它我在Console项目中引用了System.Configuration.ConfigurationManager -Version 4.5.0 ,它使用了测试类。

该错误基本上告诉您它无法在输出路径中找到该程序集,您在.net标准项目中引用它,但它也需要在启动项目的控制台应用程序中引用。

话虽如此,还要确保您的配置文件<appSettings>部分位于<configuration>部分下,例如:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <appSettings>
        <add key="Test" value="test" />
    </appSettings>
</configuration>

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM