Configuration from App.config isn't being pulled correctly

纵然是瞬间 提交于 2019-12-01 19:05:45

问题


I'm trying to extract a URL I saved to the app.config file, but it's returning a blank string. Any ideas why?

string asdf = String.Format("{0}", ConfigurationManager.AppSettings["MemberUrl"]);

And the configuration file:

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

回答1:


All config information that your class library needs must be in the main projects App.config or web.config. In other words, if your app.config file is attached to the library it will NOT be read.

Go to the main application and add the appropriate keys/values to it's config file.




回答2:


If the app.config is part of a class library it probably isn't being copied to the bin folder properly (if at all).

The config file must be named <exefilename>.config for it to be picked up by the running application.

The App.config file in the application project (the one that produces an exe file, Console, WinForms, etc.) will copy and rename on deployment. Or if this is being executed from a web project it needs to go in the web.config.

Does this help?




回答3:


Sergio I just tried this is a console application and it works perfectly.

I would suggest that it's a class library; and not a main assembly that you have added your app.config file to.

When you do a build; look in the binary output folder Debug or Release and in there you should see a file named yourEXEfilename.config; if that file is not there then you will not get any output from the line of code you have above.

AppSettings will return a NULL string.

Hope this is of use Kind Regards Noel




回答4:


there's no reason why that wouldn't work - do you have any other pertinent info ? FYI, you dont need String.Format for what you're doing, the following is fine

string asdf = ConfigurationManager.AppSettings["MemberUrl"];


来源:https://stackoverflow.com/questions/4472232/configuration-from-app-config-isnt-being-pulled-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!