1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| public class IniFile
{
#region Membres privés
private readonly Regex regexSection = new Regex(@"\s*\[([\w\s]+)\]", RegexOptions.Compiled);
private readonly Regex regexKeyValuePair = new Regex(@"\s*(\w+)=(.*)", RegexOptions.Compiled);
#endregion
#region Constructeurs
/// <summary>
/// Crée une instance vide de IniFile, pour créer dynamiquement
/// le contenu du fichier avant de l'enregistrer.
/// </summary>
public IniFile()
{
Sections = new Dictionary<string, Dictionary<string, string>>();
}
/// <summary>
/// Crée une instance de IniFile en chargeant le fichier spécifié
/// </summary>
/// <param name="fileName"></param>
public IniFile(string fileName)
{
FileName = fileName;
Reload();
}
#endregion
#region Propriétés publiques
/// <summary>
/// Le nom du fichier
/// </summary>
public string FileName { get; set; }
/// <summary>
/// Sections du fichier INI, accessibles sous forme d'un dictionnaire de sections
/// Chaque section est elle-même un dictionnaire de propriétés
/// </summary>
public Dictionary<string, Dictionary<string, string>> Sections { get; private set; }
#endregion
#region Indexeur
/// <summary>
/// Renvoie la valeur de la propriété spécifiée dans la section spécifiée
/// </summary>
/// <param name="section">Nom de la section, ou une chaine vide</param>
/// <param name="key">Nom de la propriété</param>
/// <returns>La valeur de la propriété demandée, ou null si la
/// propriété n'existe pas dans le fichier</returns>
public string this[string section, string key]
{
get { return GetValue(section, key); }
set { SetValue(section, key, value); }
}
#endregion
#region Méthodes publiques
/// <summary>
/// Charge ou recharge le fichier indiqué par la propriété FileName
/// </summary>
public void Reload()
{
if (FileName == null)
throw new InvalidOperationException("The file name is not defined");
Sections = new Dictionary<string, Dictionary<string, string>>();
using (StreamReader rd = new StreamReader(FileName))
{
string curSection = "";
string line;
Match m;
while ((line = rd.ReadLine()) != null)
{
if (line.Trim().StartsWith(";"))
{
continue;
}
else if ((m = regexSection.Match(line)).Success)
{
curSection = m.Groups[1].Value;
}
else if ((m = regexKeyValuePair.Match(line)).Success)
{
string key = m.Groups[1].Value;
string value = m.Groups[2].Value;
Dictionary<string, string> dSection = null;
if (!Sections.TryGetValue(curSection, out dSection))
dSection = AddSection(curSection);
dSection.Add(key, value);
}
}
}
}
/// <summary>
/// Ajoute une section au fichier INI
/// </summary>
/// <param name="name">Nom de la section à ajouter</param>
/// <returns>La section créée, sous form d'un dictionnaire</returns>
public Dictionary<string, string> AddSection(string name)
{
Dictionary<string, string> section = new Dictionary<string, string>();
Sections.Add(name, section);
return section;
}
/// <summary>
/// Obtient la valeur de la propriété spécifiée dans la section spécifiée
/// Si la propriété n'est pas dans une section, passer une chaine vide
/// </summary>
/// <param name="section">Nom de la section, ou une chaine vide</param>
/// <param name="key">Nom de la propriété</param>
/// <returns>a valeur de la propriété demandée, ou null si la
/// propriété n'existe pas dans le fichier</returns>
public string GetValue(string section, string key)
{
if (section == null) section = "";
Dictionary<string, string> dSection = null;
if (Sections.TryGetValue(section, out dSection))
{
string value = null;
if (dSection.TryGetValue(key, out value))
{
return value;
}
else
{
return null;
}
}
else
{
return null;
}
}
/// <summary>
/// Définit la valeur d'une propriété dans une section donnée.
/// Pour n'indiquer aucune section, passer une chaine vide.
/// Si la section n'existe pas, elle est créée
/// </summary>
/// <param name="section">Nom de la section, ou une chaine vide</param>
/// <param name="key">Nom de la propriété</param>
/// <param name="value">Valeur de la propriété</param>
public void SetValue(string section, string key, string value)
{
if (section == null)
section = "";
Dictionary<string, string> dSection = null;
if (!Sections.TryGetValue(section, out dSection))
dSection = AddSection(section);
dSection[key] = value;
}
/// <summary>
/// Enregistre le fichier INI sous le chemin indiqué par la propriété FileName
/// </summary>
public void Save()
{
if (FileName == null)
throw new InvalidOperationException("The file name is not defined");
using (StreamWriter wr = new StreamWriter(FileName))
{
Dictionary<string, string> noSection = null;
if (Sections.TryGetValue("", out noSection))
{
foreach (string key in noSection.Keys)
{
wr.WriteLine("{0}={1}", key, Sections[""][key]);
}
wr.WriteLine();
}
foreach (string secName in Sections.Keys)
{
if (secName.Length == 0)
continue;
wr.WriteLine("[{0}]", secName);
foreach (string key in Sections[secName].Keys)
{
wr.WriteLine("{0}={1}", key, Sections[secName][key]);
}
wr.WriteLine();
}
}
}
#endregion
} |
Partager