4

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>

4 Answers4

10

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?

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
  • Nope. I already stated that the .config file is being properly copied to the bin folder. That's not the issue. –  Dec 17 '10 at 16:03
  • 1
    You said "Yeah I see a SharpDIC.Api.dll.config file", But that's the DLL, not the EXE. – Colin Mackay Dec 17 '10 at 16:05
  • But there is no executable file. This is just a class library project with a App.config to save constants. –  Dec 17 '10 at 16:07
  • 2
    Then what executes the code in the class library? It goes in the config file for that application. – Colin Mackay Dec 17 '10 at 16:09
  • Why did this get voted down? It appears to me to be an expanded answer over the one that was actually accepted. And, it was posted a few minutes before that one too. *very confused* – Colin Mackay Dec 17 '10 at 16:17
  • 1
    Agreed, this answer provides a better explanation that the accepted one. – argibson Dec 17 '10 at 16:39
  • I have a console app and this line fixed it for me _**The config file must be named .config for it to be picked up by the running application**_ – Eugene Niemand Apr 19 '16 at 15:18
3

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.

NotMe
  • 87,343
  • 27
  • 171
  • 245
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

Bigtoe
  • 3,372
  • 1
  • 31
  • 47
  • Can I also ask WHY THE VOTE DOWN. It's also a more expansive suggestion than the one that was accepted as an answer and I took the time to test this out. Just like a couple of others here also who got voted down for NO reason at all; who's looking to hack down others then eh! I think it's time to report to the moderator? – Bigtoe Dec 17 '10 at 16:26
0

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"];
Dean Chalk
  • 20,076
  • 6
  • 59
  • 90