App.config中的配置未正确提取

4 c# app-config

我正在尝试提取我保存到app.config文件的URL,但它返回一个空字符串.有什么想法吗?

string asdf = String.Format("{0}", ConfigurationManager.AppSettings["MemberUrl"]);
Run Code Online (Sandbox Code Playgroud)

和配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ForumUrl" value="http://www.dreamincode.net/forums/xml.php?showforum=" />
    <add key="MemberUrl" value="http://www.dreamincode.net/forums/xml.php?showuser=" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Col*_*kay 10

如果app.config是类库的一部分,则可能没有正确复制到bin文件夹(如果有的话).

必须为配置文件命名<exefilename>.config,以便正在运行的应用程序选择它.

应用程序项目中的App.config文件(生成exe文件,控制台,WinForms等的文件)将在部署时复制和重命名.或者如果这是从Web项目执行的,则需要进入web.config.

这有帮助吗?

  • 那么是什么在类库中执行代码?它位于该应用程序的配置文件中. (2认同)