Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#

I have an example from the article.

here is the App.config file:
XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
    <add key="Setting1" value="Very" />
    <add key="Setting2" value="Easy" />
 </appSettings>
</configuration>


here is the console program write.cs:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace AppSettings
{
   class Program
   {
     static void ShowConfig()
     {

        // For read access you do not need to call OpenExeConfiguraton
        foreach(string key in ConfigurationManager.AppSettings)
        {
           string value = ConfigurationManager.AppSettings[key];
           Console.WriteLine("Key: {0}, Value: {1}", key, value);
        }
     }

     static void Main(string[] args)
     {

        ShowConfig();

        // Open App.Config of executable
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (ConfigurationUserLevel.None);

        // Add an Application Setting.
        config.AppSettings.Settings.Add("ModificationDate",
                       DateTime.Now.ToLongTimeString() + " ");

        // Save the changes in App.config file.
        config.Save(ConfigurationSaveMode.Modified);

        // Force a reload of a changed section.
        ConfigurationManager.RefreshSection("appSettings");
        ShowConfig();
      }
   }
}


What I have tried:

I am expecting this ModificationDate shall come up in App.config, but it turns out in write.exe.config file. why did this happen? the new variable can not be added into App.Config directly?
Posted
Updated 1-Mar-16 3:48am
Comments
ZurdoDev 1-Mar-16 9:33am    
Write.exe.config actually IS your app.config file. It takes the name of the executable.
CHill60 1-Mar-16 9:41am    
That's the solution :)
ZurdoDev 1-Mar-16 9:47am    
I know, but it seemed to easy.

1 solution

As mentioned in the comments, Write.exe.config actually IS your app.config file. It takes the name of the executable.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Mar-16 10:34am    
5ed, but I'm not sure the inquirer understands that. If one understood the purpose of config, this person would find it questionable, if tho different .EXE file had the same app.config; how the runtime would find it? And would not the mere observation of renamed file in output directory immediately give the idea? :-)
The question is some indication of having no proper understanding of what is the source code and what goes to runtime...
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900