Settings.Default.Properties.Add

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 - 23 nov. 2005 à 14:12
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 - 2 déc. 2005 à 18:40
bonjour,



je souhaiterais ajouter dynamiquement des parametres dans mon fichier de config...

Tout se passe bien si je les créé à la main, mais là, les noms seront
dynamiques, et C# (2005 Beta 2) me laisse ajouter mon Item :



SettingsProperty newProp = new SettingsProperty("maProp");

Properties.Settings.Default.Properties.Add(newProp);

Properties.Settings.Default.Upgrade();



mais lorsque je tente d'y accéder :



Properties.Settings.Default["maProp"] = "Value";



j'ai une exception qui se lève, me disant que je joues avec une reference nulle... : ma propriété n'existe pas


auriez vous une suggestion ?


Amusez-vous !
Renfield - thomas_reynald@msn.com
Admin CodeS-SourceS - MVP Visual Basic

5 réponses

sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
24 nov. 2005 à 10:22
as-tu essayé en utilisant

Properties.Settings.Default.Properties["maProp"] à la place de :
Properties.Settings.Default["maProp"] ?

Sébastien FERRAND
[MVP C#]
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
2 déc. 2005 à 15:17
j'ai pu avancer un peu...



SettingsProperty newProp = new SettingsProperty("maProp");

newProp.PropertyType = Type.GetType("System.String");



newProp.SerializeAs = SettingsSerializeAs.String;

newProp.DefaultValue = "";


newProp.Provider =
Releases.Properties.Settings.Default.Providers["LocalFileSettingsProvider"];




SettingsPropertyValue newPropValue = new SettingsPropertyValue(newProp);

newPropValue.PropertyValue = "maValue";



Properties.Settings.Default.Properties.Add(newProp);

Properties.Settings.Default.PropertyValues.Add(newPropValue);

Properties.Settings.Default.Save();




MessageBox.Show(Properties.Settings.Default.PropertyValues["maProp"].PropertyValue.ToString());


la ca fonctionne, je peux acceder a 'maProp' dans mon programme.

Les deux lignes en rouge, permettent de sauvegarder la configuration. Seule chose, Save lève une exception :



ConfigurationErrorsException

The setting {0} does not have either an ApplicationScopedSettingAttribute or UserScopedSettingAttribute.


Amusez-vous !
Renfield - thomas_reynald@msn.com
Admin CodeS-SourceS - MVP Visual Basic
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
2 déc. 2005 à 18:08
un p'tit truc qui fait qu'on voit que tu viens du VB :)
newProp.PropertyType = Type.GetType("System.String");

peut s'écrire en C# en :
newProp.PropertyType = typeof(string);

Concernant ton problème "ConfigurationErrorsException
The setting {0} does not have either an ApplicationScopedSettingAttribute or UserScopedSettingAttribute."

As-tu essayé d'ajouter à la collection Attributes un attribute d'un des types spécifiés ?

Exemple :
newProp.Attributes.Add(new ApplicationScopedSettingAttribute());

Sébastien FERRAND
[MVP C#]
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
2 déc. 2005 à 18:13
J'ai bien essayé, mais sans succès...

cet attribut est indiqué comme ceci, pour une des données créée via l'interface :

[global::System.Configuration.ApplicationScopedSettingAttribute()]



j'ai pas pu en faire grand chose de suffisament 'poli' pour qu'il me laisse sauvegarder en paix.



le typeof, merci, je connaissais pas, je sais pas si c'est parce que je
"viens" de VB.... je fais comme je peux avec ce qu'intellisense
m'indique ^^

Amusez-vous !
Renfield - thomas_reynald@msn.com
Admin CodeS-SourceS - MVP Visual Basic
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
2 déc. 2005 à 18:40
0
Rejoignez-nous