- /* LECTURE D'UNE VARIABLE */
- public string LireFile(string fichier, string variable)
- {
- string valeur = "";
- FileInfo fi = new FileInfo(fichier);
-
- /* on verifie si le fichier existe puis on ouvre l'acces au fichier */
- if (fi.Exists)
- {
- string s; string[] text;
- FileStream fs = new FileStream(fichier, FileMode.Open);
- StreamReader sr = new StreamReader(fs);
-
- /* On crée une boucle qui compare les variables lignes par lignes */
- /* jusqu'a trouver la variable demandée */
- do
- {
- s = sr.ReadLine();
- if ((s != null) && (s.StartsWith(variable) == true))
- {
- text = s.Split(new char[]{':'});
- valeur = text[1].Trim();
- }
- } while (s != null);
-
- }
-
- /* On retourne la valeur de la variable (retourne chaine vide si pas trouvé) */
- return valeur;
-
- }
-
-
- /* AJOUTER UNE VARIABLE ET SA VALEUR */
- public void EcrireFile(string fichier, string variable, string valeur)
- {
-
- string s = "";
- FileInfo fi = new FileInfo(fichier);
-
- /* On recupère le texte complet du fichier */
- if (fi.Exists) // on verifie ke le fichier existe
- {
- StreamReader sr = new StreamReader(fichier, ASCIIEncoding.Default);
- s = sr.ReadToEnd();
- sr.Close();
- }
-
- /* si le fichier est vide ou si il n'existe pas on le crée */
- /* et on ajoute la variable et sa valeur */
- if (s == null || s == "")
- {
- StreamWriter sw = new StreamWriter(fichier, false, ASCIIEncoding.Default);
- sw.Write(variable + " : " + valeur);
- sw.Close();
- }
- else // Sinon on ajoute simplement la ligne apres celle existante
- {
- StreamWriter sw = new StreamWriter(fichier, true, ASCIIEncoding.Default);
- sw.Write("\r\n" + variable + " : " + valeur);
- sw.Close();
- }
-
- }
-
-
- /* MODIFICATION DE LA VALEUR D'UNE VARIABLE */
- public void ModifFile(string fichier, string variable, string valeur)
- {
-
- FileInfo fi = new FileInfo(fichier);
-
- if (fi.Exists) // si le fichier existe
- {
-
- /* On met la totalité du fichier dans un variable */
- StreamReader sr = new StreamReader(fichier, ASCIIEncoding.Default);
- string s = sr.ReadToEnd();
- sr.Close();
-
- /* On trouve ou est placé la variable dans le fichier */
- int i = s.IndexOf(variable);
-
- /* On trouve la fin de la ligne de cette variable */
- int n = s.IndexOf("\r\n", i);
-
- /* On récupère la chaine dans la variable p */
- string p = s.Substring(i, n - i);
-
- /* Enfon on remplace p par la variable + valeur donnée */
- s = s.Replace(p, variable + " : " + valeur);
- StreamWriter sw = new StreamWriter(fichier, false, ASCIIEncoding.Default);
- sw.Write(s);
- sw.Close();
- }
-
- }
/* LECTURE D'UNE VARIABLE */
public string LireFile(string fichier, string variable)
{
string valeur = "";
FileInfo fi = new FileInfo(fichier);
/* on verifie si le fichier existe puis on ouvre l'acces au fichier */
if (fi.Exists)
{
string s; string[] text;
FileStream fs = new FileStream(fichier, FileMode.Open);
StreamReader sr = new StreamReader(fs);
/* On crée une boucle qui compare les variables lignes par lignes */
/* jusqu'a trouver la variable demandée */
do
{
s = sr.ReadLine();
if ((s != null) && (s.StartsWith(variable) == true))
{
text = s.Split(new char[]{':'});
valeur = text[1].Trim();
}
} while (s != null);
}
/* On retourne la valeur de la variable (retourne chaine vide si pas trouvé) */
return valeur;
}
/* AJOUTER UNE VARIABLE ET SA VALEUR */
public void EcrireFile(string fichier, string variable, string valeur)
{
string s = "";
FileInfo fi = new FileInfo(fichier);
/* On recupère le texte complet du fichier */
if (fi.Exists) // on verifie ke le fichier existe
{
StreamReader sr = new StreamReader(fichier, ASCIIEncoding.Default);
s = sr.ReadToEnd();
sr.Close();
}
/* si le fichier est vide ou si il n'existe pas on le crée */
/* et on ajoute la variable et sa valeur */
if (s == null || s == "")
{
StreamWriter sw = new StreamWriter(fichier, false, ASCIIEncoding.Default);
sw.Write(variable + " : " + valeur);
sw.Close();
}
else // Sinon on ajoute simplement la ligne apres celle existante
{
StreamWriter sw = new StreamWriter(fichier, true, ASCIIEncoding.Default);
sw.Write("\r\n" + variable + " : " + valeur);
sw.Close();
}
}
/* MODIFICATION DE LA VALEUR D'UNE VARIABLE */
public void ModifFile(string fichier, string variable, string valeur)
{
FileInfo fi = new FileInfo(fichier);
if (fi.Exists) // si le fichier existe
{
/* On met la totalité du fichier dans un variable */
StreamReader sr = new StreamReader(fichier, ASCIIEncoding.Default);
string s = sr.ReadToEnd();
sr.Close();
/* On trouve ou est placé la variable dans le fichier */
int i = s.IndexOf(variable);
/* On trouve la fin de la ligne de cette variable */
int n = s.IndexOf("\r\n", i);
/* On récupère la chaine dans la variable p */
string p = s.Substring(i, n - i);
/* Enfon on remplace p par la variable + valeur donnée */
s = s.Replace(p, variable + " : " + valeur);
StreamWriter sw = new StreamWriter(fichier, false, ASCIIEncoding.Default);
sw.Write(s);
sw.Close();
}
}