Change An Application's App.Config File Dynamically in WPF C#

Introduction

This article demonstrates how to change the content of an App.Config file at runtime. We know there are various ways to change the content of a config file. But here this article demonstrates one of the ways. This article opens the config file using a stream reader, reads the entire stream from start to end and displays the content into the text box. Then changes the content and saves the file having the same extension.

Main Concept

First, read the contents of an app.config file using stream reader and change the content then write the changed content into a file using a stream writer and save the file.

Getting Started

To read and display the contents of a config file, this article takes two TextBox controls and two button controls. One TextBox displays the path and the other one displays the content. The button controls are used to read and save the congfig file. Open Visual Studio, any version from 2005 or later, then rename the application name to "readconfig", change the path if necessary and press the "OK" button. Use two TextBox Controls and two Button Controls as in the following image. Rename the button text Browse and Save.

 
 

In the Browse button's click event use the following code to open the file and read the file's content.

    private void Button_Click_1(object sender, RoutedEventArgs e)

    {

        OpenFileDialog OFD = new OpenFileDialog();

        if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)

        {

            this.txtFilename.Text = OFD.FileName;

            this.FS = new FileStream(OFD.FileName, FileMode.Open, FileAccess.ReadWrite);

            StreamReader SR = new StreamReader(FS);

            string data = SR.ReadToEnd();

            this.txtContent.Text = data;

            FS.Flush();

            FS.Dispose();

            FS.Close();

        }

    }

The preceding first line of code uses an Open File Dialog Box. The Open File Dialog Box does not exist in WPF, to use an OpenFileDialog control you need to reference the System.Windows.Forms namespace. A OpenFileDialog box is used to get the path of the config file. Open the file using the FileStream class and read the stream using the StreamReader class as in the code above. The FileStream and StreamReader classes are available in the System.IO namespace. After retrieving the stream from the config file close and flush the StreamReader and FileStream classes, retrieve the string form the stream reader using the ReadToEnd function of the StreamReader class and set the text of the TextBox to the contents of the config file.

In the click event of the save button write the following code.

    private void Button_Click_2(object sender, RoutedEventArgs e)

    {

        this.FS = new FileStream(txtFilename.Text.Trim(), FileMode.Truncate,
FileAccess.ReadWrite);

        StreamWriter SW = new StreamWriter(this.FS);

        SW.Write(this.txtContent.Text.Trim());

        SW.Flush();

        FS.Flush();

        FS.Dispose();

        FS.Close();
 }

The preceding event opens the app.config file again and, using the stream writer, writes the changed text that is taken from text box into the app.config file.

 
 

The preceding images shows the contents of the config file.

Summery

In this article we learned how to change the contents of an app.config file dynamically using stream classes (StreamReader and StreamWriter).

Up Next
    Ebook Download
    View all
    Learn
    View all