Often when writing a program you find yourself in need of having a setting or preferences file to store your program preferences in. There are many different ways to store settings for your program including:
- Using a database file to store settings. If your program uses a database this may be a good place to store your settings if not one of the other options below may be better for you.
- Storing the settings in your App.Config file is fairly easy but doesn’t persist your settings across a program upgrade or uninstall and reinstall.
- Storing the settings in a text file is also fairly easy to do but that requires a little more programming and program logic than using an XML file.
- Using an XML file to save settings is simple and can persist data across upgrades, program uninstallation and reinstallation. If the XML file is created by the program itself and is not part of the installation set up or listed as a required file by the setup program then the XML settings file is not deleted by uninstalling the program unless the user specifically deletes the file.
This article illustrates use of an XML file to store the settings for your program. The sample project “Sample_XML_Settings” consists of a simple main form that reads from and writes to a Settings.xml file using some simple routines that are in the code file “AppSettings.vb”.
Upon running the program the first thing the program does is check the settings XML file to see if it contains all the elements and values it is supposed to have, if an element does not exist the program creates it and if a value is blank it writes the default value for it. If the settings XML file itself does not exist the program creates it.
The sample project is small enough and the code is commented so further explanation is not needed for this article.
Download the sample vb project Sample_XML_Settings.vbproj.zip