Adding property to the app config file

S

Sharon

Hello helpers,

I wish to a property setting to the application config file, So I did the
following:

System.Reflection.Assembly ass =
System.Reflection.Assembly.GetExecutingAssembly();
string fullProcessPath = ass.Location;
System.Reflection.AssemblyName assemblyName = ass.GetName();
string desiredDir = Path.GetDirectoryName(fullProcessPath);
string configPath = desiredDir + "\\" + assemblyName.Name + ".exe";

System.Configuration.SettingsProperty property = new SettingsProperty();
System.Configuration.SettingsProperty property = new
System.Configuration.SettingsProperty("LastDbFile");
property.DefaultValue = (object)"27122009.db";
property.IsReadOnly = false;
property.PropertyType = typeof(string);
property.Provider =
Properties.Settings.Default.Providers["LocalFileSettingsProvider"]
property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
if( Properties.Settings.Default.Properties["LastDbFile"] == null )
{
SettingsPropertyValue newPropValue = new SettingsPropertyValue(property);
newPropValue.PropertyValue = "27122009.db";

Properties.Settings.Default.Properties.Add(property);
Properties.Settings.Default.PropertyValues.Add(newPropValue);
}
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();

But the config file on disk does no change and so new property does not show
in this config file.

What am I doing wrong?
 
S

Sharon

Thanks Arnold, but the config file on disk simply does not changed.


I have found at the MSDN for ApplicationSettingsBase.Save() it says:
“If the only settings defined are application-scoped settings, Save will
have no effect and return no error if called with the default
LocalFileSettingsProvider. LocalFileSettingsProvider only saves user-scoped
settings.â€

So it looks like this is the reason the new property is not saved in the
config file.
Note that the new property is add to the config setting in memory (check it
by iterating over its properties).

Yet; the MSDN also say for the SettingsProvider:
“The .NET Framework contains a single default settings provider,
LocalFileSettingsProvider, which stores configuration data to the local file
system.â€


How can I make it work?

--
Thanks
Sharon


Mr. Arnold said:
Sharon said:
Hello helpers,

I wish to a property setting to the application config file, So I did the
following:

System.Reflection.Assembly ass =
System.Reflection.Assembly.GetExecutingAssembly();
string fullProcessPath = ass.Location;
System.Reflection.AssemblyName assemblyName = ass.GetName();
string desiredDir = Path.GetDirectoryName(fullProcessPath);
string configPath = desiredDir + "\\" + assemblyName.Name + ".exe";

System.Configuration.SettingsProperty property = new SettingsProperty();
System.Configuration.SettingsProperty property = new
System.Configuration.SettingsProperty("LastDbFile");
property.DefaultValue = (object)"27122009.db";
property.IsReadOnly = false;
property.PropertyType = typeof(string);
property.Provider =
Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
if( Properties.Settings.Default.Properties["LastDbFile"] == null )
{
SettingsPropertyValue newPropValue = new SettingsPropertyValue(property);
newPropValue.PropertyValue = "27122009.db";

Properties.Settings.Default.Properties.Add(property);
Properties.Settings.Default.PropertyValues.Add(newPropValue);
}
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();

But the config file on disk does no change and so new property does not show
in this config file.

What am I doing wrong?

If this is an executable program such as a console application, Windows
desktop *.exe, etc, then the config file that's going to reflect any
changes based on you doing anything via your program to the app.config
will be in programname.config where the programname.exe is located.
.
 
P

Peter Duniho

Sharon said:
Peter – I did not post this question twice.

Of course you did. I would not have asked you to not do so if you hadn't.

Here is the post to which I replied:
http://groups.google.com/group/micr...ages.csharp/msg/17d835ad32c5b525?dmode=source

Here is the post that started this particular thread:
http://groups.google.com/group/micr...ages.csharp/msg/8fb99341aecc214d?dmode=source

The only difference is the "Subject:" for the post.
Any help will be appreciated.

You already seem to have the answer. What else are you looking for?
You should be specific.

Pete
 
K

kndg

Sharon said:
Hello helpers,

I wish to a property setting to the application config file, So I did the
following:

System.Reflection.Assembly ass =
System.Reflection.Assembly.GetExecutingAssembly();
string fullProcessPath = ass.Location;
System.Reflection.AssemblyName assemblyName = ass.GetName();
string desiredDir = Path.GetDirectoryName(fullProcessPath);
string configPath = desiredDir + "\\" + assemblyName.Name + ".exe";

System.Configuration.SettingsProperty property = new SettingsProperty();
System.Configuration.SettingsProperty property = new
System.Configuration.SettingsProperty("LastDbFile");
property.DefaultValue = (object)"27122009.db";
property.IsReadOnly = false;
property.PropertyType = typeof(string);
property.Provider =
Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
if( Properties.Settings.Default.Properties["LastDbFile"] == null )
{
SettingsPropertyValue newPropValue = new SettingsPropertyValue(property);
newPropValue.PropertyValue = "27122009.db";

Properties.Settings.Default.Properties.Add(property);
Properties.Settings.Default.PropertyValues.Add(newPropValue);
}
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();

But the config file on disk does no change and so new property does not show
in this config file.

What am I doing wrong?

Hi Sharon,

In case you haven't get is solved...
As others had pointed out, application property is readonly but you may
get around this by manipulating application config file directly
provided you have sufficient write permission in the application
directory. Below is the sample code (you need to add a reference to
"System.Configuration.dll"),

private static void ApplyDbSetting()
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

if (config.AppSettings.Settings["LastDbFile"] == null)
{
config.AppSettings.Settings.Add("LastDbFile", "27122009.db");
config.Save(ConfigurationSaveMode.Modified);
}
}

But the more corect way is not to store a modifiable setting in
application config file. Instead, save it in the user config file. Just
derived a class from ApplicationSettingsBase and apply a
UserScopedSettingAttribute() to your property. Example below,

public class MyUserSettings : ApplicationSettingsBase
{
[UserScopedSetting]
[DefaultSettingValue(String.Empty)]
public string LastDbFile
{
get
{
return this["LastDbFile"].ToString();
}
set
{
this["LastDbFile"] = value;
}
}
}

Then in your main form class, have a below method,

private static void ApplyDbSetting()
{
var userSettings = new MyUserSettings();

if (String.IsNullOrEmpty(userSettings.LastDbFile))
{
userSettings.LastDbFile = "27122009.db";
userSettings.Save();
}
}

The location of config file varies between OS, but in Windows XP, it is
usually in "C:\Documents and Settings\[User Name]\Local
Settings\Application Data\[Company Name]\[Application
Name]\[Version]\user.config"

Regards,
Happy New Year!
 
S

Sharon

What config file are you talking about? Is it app.config or
programname.config.

- The config file with the name of the executable file name added with
..config.
For instance: if the executable file name is myApp.exe, then the config
file name will be myApp.exe.config.
What type of program is this, exe or dll?

- It’s an exe
 
S

Sharon

Strange, I don’t remember doing that…

I do have an answer, but it doesn’t seem to work.
 
S

Sharon

I have created a project just for this testing called ‘AddPrprptyAppSetting’
which produce the executable AddProprertyAppSetting.exe.
I’m using the Application setting wizard Settings.settings found in the
under the Properties of the project solution explorer.
With it I have added some properties that was added to file app.config which
is auto generated.
At the compilation, this same app.config is copied to the output directory
and named AddProprertyAppSetting.exe.config and it look like that:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AddProprertyAppSetting.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AddProprertyAppSetting.Properties.Settings>
<setting name="Param1" serializeAs="String">
<value>1111</value>
</setting>
<setting name="Param2" serializeAs="String">
<value>2010-01-01</value>
</setting>
<setting name="Param3" serializeAs="String">
<value>33</value>
</setting>
</AddProprertyAppSetting.Properties.Settiangs>
</applicationSettings>
</configuration>

In my code I’m using the propertied in the AddProprertyAppSetting.exe.config
as follow:

string param1 = Properties.Settings.Default.Param1;

And now I wish to add a new property so the
AddProprertyAppSetting.exe.config will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AddProprertyAppSetting.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AddProprertyAppSetting.Properties.Settings>
<setting name="Param1" serializeAs="String">
<value>1111</value>
</setting>
<setting name="Param2" serializeAs="String">
<value>2010-01-01</value>
</setting>
<setting name="Param3" serializeAs="String">
<value>33</value>
</setting>
<setting name="LastDbFile" serializeAs="String">
<value>27122009.db</value>
</setting>
</AddProprertyAppSetting.Properties.Settiangs>
</applicationSettings>
</configuration>

The ConfigurationManager does change the config file, but it adds the new
property to the file
In the appSettings section instead in the applicationSettings as follow:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AddProprertyAppSetting.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="LastDbFile" value="27122009.db" />
</appSettings>
<applicationSettings>
<AddProprertyAppSetting.Properties.Settings>
<setting name="Param1" serializeAs="String">
<value>1111</value>
</setting>
<setting name="Param2" serializeAs="String">
<value>2010-01-01</value>
</setting>
<setting name="Param3" serializeAs="String">
<value>33</value>
</setting>
</AddProprertyAppSetting.Properties.Settiangs>
</applicationSettings>
</configuration>

Using and deriving the ApplicationSettingsBase as you posted also does not
change the AddProprertyAppSetting.exe.config although it does add the new
property in runtime in memory.


Is it possible to change the AddProprertyAppSetting.exe.config at runtime by
adding the new LastDbFile property so later on in the code I can use the new
property like
string lastDbFile = Properties.Settings.Default.LastDbFile;

???
 
J

J.B. Moreno

Sharon said:
Is it possible to change the AddProprertyAppSetting.exe.config at runtime by
adding the new LastDbFile property so later on in the code I can use the new
property like
string lastDbFile = Properties.Settings.Default.LastDbFile;

???

No.

At least not as you're asking the question. You're asking if the
config file can have a new property added at runtime and for your code
to able to reference it by an unknown (to the code) name.

You can do 1 of 2 things: (1) add the property at design time (using
the Settings tab of the project properties screen) with an empty value,
and then fill it in later, or (2) access the value outside of the
Properties.Settings context (you can read the config file as just
another XML file) and get what you want out of it that way.


Which method you use is really up to you -- (1) doesn't allow you to
persist the information at runtime (it's readonly as far as the
application is concerned), (2) is more work but at least opens up the
possibility of saving changes (and with a name like lastDBFile I assume
that it can change at least somewhat frequently).
 
K

kndg

Sharon said:
I have created a project just for this testing called ‘AddPrprptyAppSetting’
which produce the executable AddProprertyAppSetting.exe.
I’m using the Application setting wizard Settings.settings found in the
under the Properties of the project solution explorer.
With it I have added some properties that was added to file app.config which
is auto generated.
At the compilation, this same app.config is copied to the output directory
and named AddProprertyAppSetting.exe.config and it look like that:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AddProprertyAppSetting.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AddProprertyAppSetting.Properties.Settings>
<setting name="Param1" serializeAs="String">
<value>1111</value>
</setting>
<setting name="Param2" serializeAs="String">
<value>2010-01-01</value>
</setting>
<setting name="Param3" serializeAs="String">
<value>33</value>
</setting>
</AddProprertyAppSetting.Properties.Settiangs>
</applicationSettings>
</configuration>

In my code I’m using the propertied in the AddProprertyAppSetting.exe.config
as follow:

string param1 = Properties.Settings.Default.Param1;

And now I wish to add a new property so the
AddProprertyAppSetting.exe.config will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AddProprertyAppSetting.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AddProprertyAppSetting.Properties.Settings>
<setting name="Param1" serializeAs="String">
<value>1111</value>
</setting>
<setting name="Param2" serializeAs="String">
<value>2010-01-01</value>
</setting>
<setting name="Param3" serializeAs="String">
<value>33</value>
</setting>
<setting name="LastDbFile" serializeAs="String">
<value>27122009.db</value>
</setting>
</AddProprertyAppSetting.Properties.Settiangs>
</applicationSettings>
</configuration>

The ConfigurationManager does change the config file, but it adds the new
property to the file
In the appSettings section instead in the applicationSettings as follow:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AddProprertyAppSetting.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="LastDbFile" value="27122009.db" />
</appSettings>
<applicationSettings>
<AddProprertyAppSetting.Properties.Settings>
<setting name="Param1" serializeAs="String">
<value>1111</value>
</setting>
<setting name="Param2" serializeAs="String">
<value>2010-01-01</value>
</setting>
<setting name="Param3" serializeAs="String">
<value>33</value>
</setting>
</AddProprertyAppSetting.Properties.Settiangs>
</applicationSettings>
</configuration>

Using and deriving the ApplicationSettingsBase as you posted also does not
change the AddProprertyAppSetting.exe.config although it does add the new
property in runtime in memory.


Is it possible to change the AddProprertyAppSetting.exe.config at runtime by
adding the new LastDbFile property so later on in the code I can use the new
property like
string lastDbFile = Properties.Settings.Default.LastDbFile;

???

Ahh, okay... now I understand what you are trying to do...
From your post, you are using the IDE's Settings editor to generate the
config file. My previous post target the config file using custom class.
You should use either one (IDE or custom class) but not recommended to
use both.

If you are using IDE's Settings editor, Visual Studio will
auto-generated the class for you and you can check it by double-clicking
the Settings.Designer.cs. If you look at it, the IDE will generate a
class named Settings under [YourProjectName].Properties namespace. It
will also add a property correspond to settings that you had added. That
why you have the convenience to get/set your setting by calling
Properties.Settings.Default.[YourSetting].

As for the answer to your question, it would be no. As I said
previously, application scope setting is readonly. If you want it to
persist between session, you have to set it as a user scope setting.
This setting however will not be saved on application config file.
Instead, it will saved on the user config file for which the location of
the file is mentioned in my previous post.

You can also manipulate the application config file directly by using
ConfigurationManager or XmlWriter or your own parser but you will lost
the convenience to set your setting using
Properties.Setting.Default.[YourSetting] as this property is
auto-generated by the IDE's Setting editor.

Regards.
 

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

Similar Threads

Interesaznt 1
Dynamic Settings 2

Top