at first I would like to try to explain my plan: Sometimes I just have to change some static values in my code (lets say, the path to a folder with data my program needs or the url to upload something) and after that I have to compile again. If lets say the url is changed where I want to upload the files and the url is like I said static in my code – well I have to change it in Visual Studio and compile it again…and again and again. Well thats why I think this is NOT a good idea. It would be better to make a config file (a file where I can store all my values/information that I need) and read this file every time my program is starting. Now I could make changes without compiling – I just have to edit my config file.
I thought I choose the XML format, I could take an normal textfile, but I think an XML is more secure and easier to read if it gets complex. Now there is maybe one problem with this: I have to ‘hardcode’ the path to my XML file. I think I will never change the path to the config file, but what if I would? Then I have to change the path in my code again. Is there maybe a smarter solution for this?
The next step would be that I have to read the XML file on every startup. Now I would like to know what is the best OOP solution to get the information and use it? I thought I make a class (lets say Myconfig) and in this class I have only final variables (I think final is readonly in c#) with getter and setter. I will create a new Myconfig on every startup and I will parse the XML file – while Im parsing and getting the information Im using the setter to save the information in the class variable. After Im done I have an object with all the information and now I could send/use this object everywhere to get these information (let say with Myconfig.Url , Myconfig.Path …).
Well thats my plan 🙂 Im really new to this, Im sure this is not perfect. Any suggestions?
use a configuration file with the ConfigurationManager class to access values in it. .NET has already provided this solution for you – there is no reason for you to write one yourself.