P
Peted
Hi,
im wanting to store some custom text strings in the app.config file of
a c# app, to be retreived and updated when the app runs.
using c# 2005 express
in my testing i am using the code bellow, and its almost idenictal to
every web example i have found. They all suggest the values and file
can be read from and updated. The code bellow runs, with no errors of
any kind but the app.config file never actually gets updated.
everytime you run the code it always loads the same data in app.config
i cannot work out why the file is not updated with new information
Note that i not only want to add new keys to the file i will also what
to update exisiting keys as well
any advice appreciated
using System;
using System.Collections.Specialized;
using System.Configuration;
namespace ConfigurationSamples
{
class UsingAppSettingsSection
{
static void ShowAppSettings()
{
string[] names = ConfigurationManager.AppSettings.AllKeys;
NameValueCollection appStgs =
ConfigurationManager.AppSettings;
for (int i = 0; i < appStgs.Count; i++)
{
Console.WriteLine("#{0} Name: {1} Value: {2}",
i, names, appStgs);
}
}
static void Main(string[] args)
{
Console.WriteLine("Application Settings Before:");
ShowAppSettings();
Console.WriteLine();
Console.WriteLine("Add an Application Setting.");
// Get the count of the Application Settings.
int appStgCnt = ConfigurationManager.AppSettings.Count;
string asName = "AppStg" + appStgCnt.ToString();
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add(asName,
DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString());
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine();
Console.WriteLine("Application Settings After:");
ShowAppSettings();
Console.WriteLine();
// Show the value of a named Application Setting.
Console.WriteLine(
"The value of application setting {0} is '{1}'.",
asName, ConfigurationManager.AppSettings[asName]);
Console.WriteLine();
Console.WriteLine("Press 'Enter' to exit.");
Console.ReadLine();
}
}
}
im wanting to store some custom text strings in the app.config file of
a c# app, to be retreived and updated when the app runs.
using c# 2005 express
in my testing i am using the code bellow, and its almost idenictal to
every web example i have found. They all suggest the values and file
can be read from and updated. The code bellow runs, with no errors of
any kind but the app.config file never actually gets updated.
everytime you run the code it always loads the same data in app.config
i cannot work out why the file is not updated with new information
Note that i not only want to add new keys to the file i will also what
to update exisiting keys as well
any advice appreciated
using System;
using System.Collections.Specialized;
using System.Configuration;
namespace ConfigurationSamples
{
class UsingAppSettingsSection
{
static void ShowAppSettings()
{
string[] names = ConfigurationManager.AppSettings.AllKeys;
NameValueCollection appStgs =
ConfigurationManager.AppSettings;
for (int i = 0; i < appStgs.Count; i++)
{
Console.WriteLine("#{0} Name: {1} Value: {2}",
i, names, appStgs);
}
}
static void Main(string[] args)
{
Console.WriteLine("Application Settings Before:");
ShowAppSettings();
Console.WriteLine();
Console.WriteLine("Add an Application Setting.");
// Get the count of the Application Settings.
int appStgCnt = ConfigurationManager.AppSettings.Count;
string asName = "AppStg" + appStgCnt.ToString();
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add(asName,
DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString());
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine();
Console.WriteLine("Application Settings After:");
ShowAppSettings();
Console.WriteLine();
// Show the value of a named Application Setting.
Console.WriteLine(
"The value of application setting {0} is '{1}'.",
asName, ConfigurationManager.AppSettings[asName]);
Console.WriteLine();
Console.WriteLine("Press 'Enter' to exit.");
Console.ReadLine();
}
}
}