Reputation:
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>
Upvotes: 4
Views: 5563
Reputation: 19175
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?
Upvotes: 10
Reputation: 3510
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
Upvotes: 3
Reputation: 88044
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.
Upvotes: 3
Reputation: 20451
there's no reason why that wouldn't work - do you have any other pertinent info ?
FYI, you dont need String.Forma
t for what you're doing, the following is fine
string asdf = ConfigurationManager.AppSettings["MemberUrl"];
Upvotes: 0