trouble writing new and updated values to app.config file

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();
}
}
}
 
G

Guest

Hi,

When i runned your code two times my config file had been like this.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="AppStg0" value="22 Ağustos 2007 Çarşamba 11:23:34" />
<add key="AppStg1" value="22 Ağustos 2007 Çarşamba 11:24:50" />
</appSettings>
</configuration>

isn't this true?


"Peted", iletisinde şunu yazdı,
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();
}
}
}
 
P

Peted

I do not get the result at all of what you show bellow.
I get no updates to the app.config file at all.

I dont get any error messages, the program seems to run fine
but it makes no changes to the app.config file.

How is it that it worked for you.

I wonder if c# express 2005 edition is limited in this functionality ?
 
G

Guest

Hi,
When i runned program without adding app.config, its automatically created a
config file that its name ProgramName.exe.Config and located in bin/debug
directory.

Did you look this file?

I am not sure is there any limitations beetween c# express and vs2005; but i
don't think so.

"Peted", iletisinde şunu yazdı,
 
P

Peted

Yes ive looked there and i accidently realised somehting


If i run the exe from the debugger c# ide the config file remains
unchanged. If i run the exe manually by double mouse click in the
debug directory then it does add new content to the config file.

Im not sure why it works if you run it mannually and not work if you
run it from the c# ide.

In any case all i need to work out now is how to change the value of
existing keys, to update/replace the values they hold

any hints ?
 
P

Peted

Does anyone have any idea why the app.config file only gets updated
when i run the exe directly and not updated when im testing the
program through the c# ide debugger ?

thanks
 
V

verbiest

Does anyone have any idea why the app.config file only gets updated
when i run the exe directly and not updated when im testing the
program through the c# ide debugger ?

thanks










- Tekst uit oorspronkelijk bericht weergeven -

Maybe because the debugger actually runs a different exe (look in task
manager, you'll see an exe called *.vshost.exe). So maybe you're
actually updating the *.vshost.exe.config file?

regards,
Kristof Verbiest
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top