By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

trouble writing new and updated values to app.config file

P: n/a
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[i], appStgs[i]);
}
}

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(Configur ationUserLevel.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();
}
}
}
Aug 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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ı,
news:e7********************************@4ax.com...
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[i], appStgs[i]);
}
}

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(Configur ationUserLevel.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();
}
}
}

Aug 22 '07 #2

P: n/a
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 ?


On Wed, 22 Aug 2007 11:27:33 +0300, Bahadir ARSLAN
<ba************@netron.com.trwrote:
>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 Agustos 2007 Çarsamba 11:23:34" />
<add key="AppStg1" value="22 Agustos 2007 Çarsamba 11:24:50" />
</appSettings>
</configuration>

isn't this true?
Aug 22 '07 #3

P: n/a
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ı,
news:rr********************************@4ax.com...
>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 ?


On Wed, 22 Aug 2007 11:27:33 +0300, Bahadir ARSLAN
<ba************@netron.com.trwrote:
>>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 Agustos 2007 Çarsamba 11:23:34" />
<add key="AppStg1" value="22 Agustos 2007 Çarsamba 11:24:50" />
</appSettings>
</configuration>

isn't this true?
Aug 22 '07 #4

P: n/a
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 ?


On Wed, 22 Aug 2007 12:08:44 +0300, Bahadir ARSLAN
<ba************@netron.com.trwrote:
>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 sunu yazdi,
news:rr********************************@4ax.com.. .
>>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 ?


On Wed, 22 Aug 2007 11:27:33 +0300, Bahadir ARSLAN
<ba************@netron.com.trwrote:
>>>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 Agustos 2007 Çarsamba 11:23:34" />
<add key="AppStg1" value="22 Agustos 2007 Çarsamba 11:24:50" />
</appSettings>
</configuration>

isn't this true?
Aug 22 '07 #5

P: n/a
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

On Wed, 22 Aug 2007 17:29:35 +0800, Peted wrote:
>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 ?


On Wed, 22 Aug 2007 12:08:44 +0300, Bahadir ARSLAN
<ba************@netron.com.trwrote:
>>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 sunu yazdi,
news:rr********************************@4ax.com. ..
>>>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 ?


On Wed, 22 Aug 2007 11:27:33 +0300, Bahadir ARSLAN
<ba************@netron.com.trwrote:

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 Agustos 2007 Çarsamba 11:23:34" />
<add key="AppStg1" value="22 Agustos 2007 Çarsamba 11:24:50" />
</appSettings>
</configuration>

isn't this true?
Aug 23 '07 #6

P: n/a
On 23 aug, 05:22, Peted wrote:
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

On Wed, 22 Aug 2007 17:29:35 +0800, Peted wrote:
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 ?
On Wed, 22 Aug 2007 12:08:44 +0300, Bahadir ARSLAN
<bahadir.ars...@netron.com.trwrote:
>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 sunu yazdi,
news:rr********************************@4ax.com.. .
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 ?
>On Wed, 22 Aug 2007 11:27:33 +0300, Bahadir ARSLAN
<bahadir.ars...@netron.com.trwrote:
>>>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 Agustos 2007 arsamba 11:23:34" />
<add key="AppStg1" value="22 Agustos 2007 arsamba 11:24:50" />
</appSettings>
</configuration>
>>>isn't this true?- Tekst uit oorspronkelijk bericht niet weergeven -

- 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

Aug 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.