Click here to Skip to main content
12,402,889 members (72,814 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.1K views
36 downloads
4 bookmarked
Posted

Basics on Debugging App.config for Windows Form Application

, 19 Mar 2016 CPOL
Rate this:
Please Sign up or sign in to vote.
One time-saving tip on debugging App.config file

Introduction

When we develop Windows Form application, we will use App.config surely. We can add, modify and delete key/value pairs to manage user preferences. Here, one tip is shared to save your time in future.

A Demo

Here is a sample Windows Form application to show how to add a key pair into App.config file. I build this Windows Form application inside Visual Studio 2015.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
namespace AppConfig
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //display current value of Setting1 variable
        private void button2_Click(object sender, EventArgs e)
        {
            //clear first
            label1.Text = "";

            DisplayConfig();
        }

        private void DisplayConfig()
        {
            foreach(string key in ConfigurationManager.AppSettings)
            {
                string value = ConfigurationManager.AppSettings[key];
                label1.Text = label1.Text + " " + key + ": " + value;                     
            }
        }

        //display current content in App.config appSettings section
        private void button1_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings.Add("ModificationTime", DateTime.Now.ToLongTimeString() + " ");
            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}

Problem

Inside Visual Studio 2015 IDE, press F5 to run this application. Press button Add time stamp into App.config and then click display button to current values in App.config file:

We can see Modification Time is added into App.config. Now, we go to the bin folder of this project and locate App.config. Open it in Notepad and you will find Modification Time is not there. Here is the problem: we see this time stamp is added into App.config, but when we open App.config file and this key/value pair is not there.

What happened?

Answer

It is related with Visual Studio IDE debugging process. When we press F5 and start debugging process, Visual Studio creates its own hosting processes. Inside project bin/debug folder, you will see projectName.vshost and projectName.vshost.exe XML files. When you run this application inside Visual Studio IDE, the Modification Time key/value pair is put into projectName.vshost.exe XML file. After you stop the debugging process, the cache is released and the Modification Time key/value pair is flushed from the XML file. So you will not see any change in this XML file.

Also please note, inside debugging process, projectName.exe.config is never used. So if we open projectName.exe.config file, we will not see any change.

Two solutions are given here:

Solution one: Start developer command line prompt and go to bin\Debug folder. Run demo application from there. You will see the Modification Time in App.config appSettings section.

Solution two: Select project property page; select Debug tab, go to bottom section "Enable Debugger", and uncheck "Enable Visual Studio hosting process" as the following screenshot:

Hope this tip can save you some time in the future. If you have any comments and feedback, please let me know.

Reference

  1. VSHOST — the Hosting Process

History

  • 03-19-2016: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Southmountain
Software Developer
United States United States
turns good thoughts into actions...

I take notes on my blog

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 20 Mar 2016
Article Copyright 2016 by Southmountain
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid