Dynamic Settings

G

Guest

I am trying to create dynamic settings in a .NET 2.0 C# application. I need
to be able to store settings on the user, but I do not know how many settings
are necessary at design time because the settings are determined by a
business object that is loaded into the app of which there could be one or
many. I would prefer to use the same LocalFileSettingsProvider that the
settings default to so that I do not have to manage it myself.

Here is the code that I currently have:

internal static void SetStringSetting(string name, bool isReadOnly, string
value)
{
SettingsProperty property = new SettingsProperty(name);
property.SerializeAs = SettingsSerializeAs.String;
property.PropertyType = typeof(string);
property.IsReadOnly = isReadOnly;
property.Provider = Properties.Settings.Default.Providers
["LocalFileSettingsProvider"];

SettingsPropertyValue propertyValue = new SettingsPropertyValue(property);
propertyValue.PropertyValue = value;

Properties.Settings.Default.PropertyValues.Add(propertyValue);
}

internal static string GetStringSetting(string name)
{
if (Properties.Settings.Default.PropertyValues[name] == null)
return String.Empty;

if (Properties.Settings.Default.PropertyValues[name].PropertyValue == null)
return String.Empty;

return
Properties.Settings.Default.PropertyValues[name].PropertyValue.ToString();
}

This code will create the settings and persist them across the application
session, however, it does not utilize the LocalFileSettingsProvider to save
them to the file. When the application tries to save at close, I also
recieve this error:

Type: ConfigurationErrorsException
Message: The setting {0} does not have either an
ApplicationScopedSettingAttribute or UserScopedSettingAttribute.

I have tried to do:

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add(scopeAttribute, scopeAttribute);

and

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add("UserScopedSettingAttribute", scopeAttribute);

but I cannot get it to work. Can anyone help me out on this problem?
 
S

Sheng Jiang[MVP]

you can handle the SettingsLoaded event and override the settings in an
upper layer.

void Settings_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
SettingNeededEventArgs settingNeededEventArgs = new
SettingNeededEventArgs();
DAL.Setting.FireSettingNeedEvent(this, settingNeededEventArgs);
if (!string.IsNullOrEmpty(settingNeededEventArgs.ConnectionString))
{
this["ConnectionString"] = settingNeededEventArgs.ConnectionString;
}
}
namespace DAL
{
public class Setting
{
static public event EventHandler<SettingNeededEventArgs> OnSettingNeeded;

internal static void FireSettingNeedEvent(RipAppData.Properties.Settings
settings, SettingNeededEventArgs settingNeededEventArgs)
{
if (OnSettingNeeded != null)
OnSettingNeeded(settings, settingNeededEventArgs);
}
}
public class SettingNeededEventArgs : EventArgs
{
private string connectionString;

public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}

}
}
handle DAL.Setting.OnSettingNeeded in your upper layer
Another approach is using the Microsoft Enterprise Library.
--
Sheng Jiang
Microsoft MVP in VC++
Aaron said:
I am trying to create dynamic settings in a .NET 2.0 C# application. I need
to be able to store settings on the user, but I do not know how many settings
are necessary at design time because the settings are determined by a
business object that is loaded into the app of which there could be one or
many. I would prefer to use the same LocalFileSettingsProvider that the
settings default to so that I do not have to manage it myself.

Here is the code that I currently have:

internal static void SetStringSetting(string name, bool isReadOnly, string
value)
{
SettingsProperty property = new SettingsProperty(name);
property.SerializeAs = SettingsSerializeAs.String;
property.PropertyType = typeof(string);
property.IsReadOnly = isReadOnly;
property.Provider = Properties.Settings.Default.Providers
["LocalFileSettingsProvider"];

SettingsPropertyValue propertyValue = new SettingsPropertyValue(property);
propertyValue.PropertyValue = value;

Properties.Settings.Default.PropertyValues.Add(propertyValue);
}

internal static string GetStringSetting(string name)
{
if (Properties.Settings.Default.PropertyValues[name] == null)
return String.Empty;

if (Properties.Settings.Default.PropertyValues[name].PropertyValue == null)
return String.Empty;

return
Properties.Settings.Default.PropertyValues[name].PropertyValue.ToString();
}

This code will create the settings and persist them across the application
session, however, it does not utilize the LocalFileSettingsProvider to save
them to the file. When the application tries to save at close, I also
recieve this error:

Type: ConfigurationErrorsException
Message: The setting {0} does not have either an
ApplicationScopedSettingAttribute or UserScopedSettingAttribute.

I have tried to do:

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add(scopeAttribute, scopeAttribute);

and

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add("UserScopedSettingAttribute", scopeAttribute);

but I cannot get it to work. Can anyone help me out on this problem?
 
G

Guest

That doesn't make any sense. I have several questions about your code:

1. How should the SettingsLoaded event be implemented? I put the event
attachment in the constructor of my form and the method generated there, but
the call: this["MySetting"] does not work because this refers to the form.

2. I don't see how loading a setting solves my problem. I'm trying to save
a setting to the file using the default LocalFileSettingsProvider so that I
can retrieve it later.

3. You don't have anything that subscribes to the OnSettingNeeded event so
I don't understand what the purpose of having it is.

Sheng Jiang said:
you can handle the SettingsLoaded event and override the settings in an
upper layer.

void Settings_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
SettingNeededEventArgs settingNeededEventArgs = new
SettingNeededEventArgs();
DAL.Setting.FireSettingNeedEvent(this, settingNeededEventArgs);
if (!string.IsNullOrEmpty(settingNeededEventArgs.ConnectionString))
{
this["ConnectionString"] = settingNeededEventArgs.ConnectionString;
}
}
namespace DAL
{
public class Setting
{
static public event EventHandler<SettingNeededEventArgs> OnSettingNeeded;

internal static void FireSettingNeedEvent(RipAppData.Properties.Settings
settings, SettingNeededEventArgs settingNeededEventArgs)
{
if (OnSettingNeeded != null)
OnSettingNeeded(settings, settingNeededEventArgs);
}
}
public class SettingNeededEventArgs : EventArgs
{
private string connectionString;

public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}

}
}
handle DAL.Setting.OnSettingNeeded in your upper layer
Another approach is using the Microsoft Enterprise Library.
--
Sheng Jiang
Microsoft MVP in VC++
Aaron said:
I am trying to create dynamic settings in a .NET 2.0 C# application. I need
to be able to store settings on the user, but I do not know how many settings
are necessary at design time because the settings are determined by a
business object that is loaded into the app of which there could be one or
many. I would prefer to use the same LocalFileSettingsProvider that the
settings default to so that I do not have to manage it myself.

Here is the code that I currently have:

internal static void SetStringSetting(string name, bool isReadOnly, string
value)
{
SettingsProperty property = new SettingsProperty(name);
property.SerializeAs = SettingsSerializeAs.String;
property.PropertyType = typeof(string);
property.IsReadOnly = isReadOnly;
property.Provider = Properties.Settings.Default.Providers
["LocalFileSettingsProvider"];

SettingsPropertyValue propertyValue = new SettingsPropertyValue(property);
propertyValue.PropertyValue = value;

Properties.Settings.Default.PropertyValues.Add(propertyValue);
}

internal static string GetStringSetting(string name)
{
if (Properties.Settings.Default.PropertyValues[name] == null)
return String.Empty;

if (Properties.Settings.Default.PropertyValues[name].PropertyValue == null)
return String.Empty;

return
Properties.Settings.Default.PropertyValues[name].PropertyValue.ToString();
}

This code will create the settings and persist them across the application
session, however, it does not utilize the LocalFileSettingsProvider to save
them to the file. When the application tries to save at close, I also
recieve this error:

Type: ConfigurationErrorsException
Message: The setting {0} does not have either an
ApplicationScopedSettingAttribute or UserScopedSettingAttribute.

I have tried to do:

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add(scopeAttribute, scopeAttribute);

and

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add("UserScopedSettingAttribute", scopeAttribute);

but I cannot get it to work. Can anyone help me out on this problem?
 

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
Adding property to the app config file 10

Top