Hello,
I copied this code from MS web site trying to add a new key to current config file, but it does not save the config file at all when my app exits, any idea?
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("TestKey", "gdhgasdkjadhsj");
// Save the configuration file.
config.Save(ConfigurationSaveMode.Full);
How could this code have no exceptions whatsoever but just not doing what Microsoft claim it should do -- save the damn config file?
Loading
Guest UserPosted Jan 7, 2008, 5:18 PM
using System;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Collections;
using System.Text;
using System.Configuration;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();
string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();
config.AppSettings.Settings.Add(newKey, newValue);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
}
}
Since the app is named "ConsoleApplication2.exe", the config file that it's saving to is "ConsoleApplication2.exe.Config", not "App.config".