Ini files avec C#

cs_ellen Messages postés 1 Date d'inscription mercredi 8 décembre 2004 Statut Membre Dernière intervention 8 décembre 2004 - 8 déc. 2004 à 16:39
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 - 8 déc. 2004 à 18:38
Comment fait-on pour lire un fichier ini (ou de configuration) avec C#?
Je souhaite avoir accès à des paramètres dans un fichier de configuration avec C#.
Merci de votre aide

2 réponses

MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
8 déc. 2004 à 17:29
Tu peux utiliser un fichier XML.

J'avais ecrit cette pette classe il y a longtemps pour une application qui allait lire des parametres

Le fichier XML ressemblait a ca :

<?xml version="1.0" encoding="utf-8"?>
<options>
  
  <dataSource path="E:\Developpement\CSharp\Exploitations\Options.xml" />
</options>


La classe :

using System;
using System.Xml;
using System.Windows.Forms;

namespace Exploitations
{
public class MyXml
{
private XmlDocument doc = null;
public MyXml(string file)
{
doc = new XmlDocument();
try
{
doc.Load(file);
}
catch(System.IO.FileNotFoundException)
{
MessageBox.Show("Le fichier " + file + " n'existe pas", "Erreur");
doc = null;
}
catch(Exception e)
{
MessageBox.Show(e.ToString(), "Erreur");
doc = null;
}
}

public string GetValue(string node, string attrib)
{
try
{
XmlNodeList list = doc.GetElementsByTagName(node);
return list.Item(0).Attributes[attrib].Value.ToString();
}
catch(Exception e)
{
MessageBox.Show(e.ToString(), "Erreur");
return "";
}
}

public void SetValue(string node, string attrib, string val)
{
try
{
if(doc != null)
{
XmlNodeList list = doc.GetElementsByTagName(node);
list.Item(0).Attributes[attrib].Value = val;
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString(), "Erreur");
}
}

public void Save(string path)
{
try
{
if(doc != null)
doc.Save(path);
}
catch(Exception e){MessageBox.Show(e.ToString(), "Erreur");}
}

public XmlDocument Doc
{
get{ return doc ;}
}

}
}


La lecture des options ainsi :

MyXml options = new MyXml(Application.StartupPath + @"\\Options.xml");
if(options.Doc != null)
{
    startMAJ.Checked = bool.Parse(options.GetValue("autoUpdate", "updateWhenOpening"));
    majTime.Text = options.GetValue("autoUpdate", "updateTimeInMinutes");
}


Et l'enregistrement des options

MyXml options = new MyXml(Application.StartupPath + @"\\Options.xml");
options.SetValue("autoUpdate", "updateWhenOpening", startMAJ.Checked.ToString());
options.SetValue("autoUpdate", "updateTimeInMinutes", majTime.Text);
options.Save(Application.StartupPath + @"\\Options.xml");
options = null;


Tu peux facilement l'optimiser pour ton utilisation :)

a++
Mx
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
8 déc. 2004 à 18:38
une source sur le sujet : GESTIONNAIRE DE FICHIERS INI

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
Rejoignez-nous