Working with Configuration Files and Visual Basic


.Net developers are quite spoilt for choice when it comes to storing little pieces of information. Common places in which developers can store data are the following:

  1. Configuration files
  2. The Registry
  3. Flat files
  4. INI files
  5. Databases

It all depends on the need of the user, and obviously the amount of data that needs storing. It is senseless saving a couple of strings into a database; it is also senseless storing huge amounts of data into anything other than a database.

Today I will show you how to store, modify and retrieve information from a Configuration file.

What are Configuration Files?

As the name implies, a Configuration file allows you to store configuration settings. These configuration settings could be anything such as a database connection, common strings, or objects that will be used throughout your entire application. The benefit of using a Config file is that it is automatically part of your application. This eliminates the need to create separate files in order to store your settings.

Configuration files are in XML format. This means that every setting stored inside a config file can easily be read if you understand the very basics of XML. Every Windows Forms application includes a file called app.config, which I will talk about now.


As said, any Windows Forms application includes this file as part of the solution - so create a VB.NET Windows Forms application quickly. You can store all your Database connection strings, Resource locations (et al.) inside it. If you were to open the App.Config file inside Visual Studio it will look like the following:

<?xml version="1.0" encoding="utf-8" ?>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

Doesn't look like much. This simply tells the application that it expects .NET Framework 4.5 in order to run. Now where do the Settings I mentioned come in?

You have to edit the App.Config file to include your desired settings. Edit your App.Config to look like the next code listing:

<?xml version="1.0" encoding="utf-8" ?>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <add key="TestKey" value="whatever"/>

All I included here was the appSettings section. Here I created a sub key named TestKey and supplied a senseless value (as this is just an example). Now, you have added a section to your Config file, and you can manipulate it through VB.NET Code. Design your form to resemble Figure 1 below.

Our Design
Figure 1 - Our Design


Before you can jump in and code, you first need to set a project reference to System.Configuration by following these steps:

  • Click Project
  • Click Add Reference
  • Click Assemblies
  • Click Framework (if necessary)
  • Scroll to System.Configuration and check the box next to it, as shown in Figure 2.

Added Project Reference
Figure 2 - Added Project Reference

Now that all the semantics are out of the way, you can start coding. As usual (I am a creature of habit) add the Imports statements first:

Imports System.Configuration 'Need to add project reference as well

The reference and the namespace allows us to be able to read any configuration file as well as to supply you with the necessary tools to do it. Create the following modular objects:

    Dim cAppConfig As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "\Config_File_Ex.exe")
    Dim asSettings As AppSettingsSection = cAppConfig.AppSettings

The first object you created (cAppConfig) is a Configuration object. You use this to open the application's config file via the use of the ConfigurationManager class' OpenExeConfiguration method. The next object is an AppSettingsSection object, which reads the appSettings key within the specified Configuration file object.

Storing a Value Inside a Configuration File

Now that everything is set up, you can finally store a value inside the appSettings key you created earlier. Add the following code behind the button labelled 'Store':

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        asSettings.Settings.Item("TestKey").Value = 5 'Save Original Value

    End Sub

Here you opened the TestKey inside appSettings and give it a value. The next line simply saves the Configuration into the Config file.

Reading from a Configuration File

Add the next code behind the button labelled 'Show':

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


            Dim appSettings = ConfigurationManager.AppSettings 'Read Stored Value
            Dim result As String = appSettings("TestKey")

            If IsNothing(result) Then

                result = "Not found"

            End If


        Catch ec As ConfigurationErrorsException

            MessageBox.Show("Error reading app settings")

        End Try

    End Sub

You created an AppSettings object to read through the Config file's AppSettings section. Then, you read from the specified key - in this case it is TestKey. If there is data present it will show the data inside a MessageBox, else, it will inform you that nothing has been stored.

Editing Config File Values

Add the next code behind the button labeled 'Edit':

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        'Re-Open file and Store a new value
        cAppConfig = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "\Config_File_Ex.exe")
        asSettings = cAppConfig.AppSettings
        asSettings.Settings.Item("TestKey").Value = 15 'Save Modified Value

    End Sub

Inside the Edit button you simply open the Configuration file and the desired key, then you store a new value and save the file again.


In this article you have learned how to manipulate data in Configuration Files. I hope you have enjoyed it as much as I did. Until next time, cheers!

Related Articles



  • Getting an error with Config_File_Ex.exe

    Posted by John on 11/05/2015 10:38am

    When I try to store a value I get an error "parameter 'exePath' is invalid". I tried to set some references on "Copy local", I search for "Config_File_Ex.exe" but cannot find it. In other words I'm not able to store my values. What to do?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date