[VS2013] ConfigurationManager - Config de mon app
bonjour,
je cherche à enregistrer les paramètres de mon applis, je pensais utiliser un fichier ini, mais c# utilise apparemment des fichiers de config.
j'ai donc trouvé ce tuto sur ce site.
j'ai donc créé ce code:
app.config
Code:
1 2 3 4 5 6 7 8 9 10 11
|
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="as1" value="aaa"/>
<add key="as2" value="bbb"/>
<add key="as3" value="ccc"/>
<add key="as4" value="ddd"/>
<add key="as5" value="eee"/>
</appSettings>
</configuration> |
Code:
1 2 3 4 5 6 7 8 9 10
|
private void Form1_Load(object sender, EventArgs e)
{
string txt = "";
foreach (string aValue in ConfigurationManager.AppSettings)
{
txt += String.Format("Key {0} - Value {1}", aValue, ConfigurationManager.AppSettings[aValue]) + Environment.NewLine;
}
MessageBox.Show(txt);
} |
à, pas de problèmes, je récupère toutes les données.
si j'essaie d'ajouter des données supplémentaires, ça plante:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="as1" value="aaa"/>
<add key="as2" value="bbb"/>
<add key="as3" value="ccc"/>
<add key="as4" value="ddd"/>
<add key="as5" value="eee"/>
</appSettings>
<dossiers>
<liste>
<add key="as01" value="aaa-aaa"/>
<add key="as02" value="bbb-bbb"/>
</liste>
</dossiers>
</configuration> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
private void Form1_Load(object sender, EventArgs e)
{
string txt = "";
foreach (string aValue in ConfigurationManager.AppSettings)
{
txt += String.Format("Key {0} - Value {1}", aValue, ConfigurationManager.AppSettings[aValue]) + Environment.NewLine;
}
MessageBox.Show(txt);
Hashtable options = (Hashtable)ConfigurationManager.GetSection("dossiers/liste");
txt = "";
foreach (DictionaryEntry opt in options)
{
txt += opt.Value;
}
MessageBox.Show(txt);
} |
avec ce code, dès la 1ère boucle (qui fonctionnait lors de mon test précédent), ça plante, les objet retourne null et génère une exception.
je ne comprends pas ce qui ne va pas dans mon code.
j'ai cherché des infos, mais je n'ai rien pour m'aider à résoudre ce pb.
j'espère que l'un de vous pourra m'éclairer.
merci de votre aide,
ben