The game has changed and configuration moving forward is to be done using json files and the beloved ConfigurationManager.AppSettings is going away. The good news: I have a quick class that will ease you into it.

Maybe, like me, you got all excited about .NET Core and thought you would just snag an existing app and dump it’s code into a .NET Core project to see what would happen. After working through some of the missing packages and modifying a few namespaces to make things work you realize you can no longer use ConfigurationManager to retrieve your AppSettings. Bummer.

I was (clearly) faced with this situation but wanted to focus on the other aspects of porting the app and not the mundane issue of configuration so I created a singleton class to handle reading my now-deprecated app.config. I call it… AppConfig. No, seriously. But feel free to make it your own as I release this code for use in whatever shenanigans you deem it worthwhile for.

First up, it’s a singleton or single-instance object that reads your app.config file from the binary’s folder on instantiation and absorbs the key/value pairs into itself since it derives from Dictionary<string, string>. I chose this as it most closely resembles the functionality of using ConfigurationManager.AppSettings (in my humble opinion).

The code? It looks like this:

OK, cool. You have the code now how do you use this thing?

Assuming you already have your app.config file in the project, make sure you set the file properties to “Copy to Output Directory” so it is there for access when we need it at runtime. Failure to do this will result in, you know, it not working since the code expects it to be where the action (execution) is.

Next, instantiate the class and use it! I do this in the beginning and access it as:

OK, I lied. I don’t really use it like this. But this is a decent example. Remember that all values are going to be strings so expect that as a return type.

So what DOESN’T this do? It doesn’t handle different sections. It doesn’t handle duplicate entries (it just takes the last one it reads as gospel and rolls with it).

That’s it! Hope this helps as a quick-fix as it did for me!

Like this post? Share it!
Free WordPress Themes, Free Android Games