// =============================================================================================================================== // ==[FONCTION]=================================================================================================================== // Fonction : ReadKey // Description : Lit une clé d'une section du fichier .ini // Argument(s) : string pSection = Nom de la section ou se trouve la clé // : string pKey = Nom de la clé à lire // Créateur(s) : BUSTOS Thibault(TheRake66) // Version : 1.0 // =============================================================================================================================== public string ReadKey(string pSection, string pKey) { string[] IniText; bool IniInSection = false; try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); } catch { return(""); } foreach (string IniLine in IniText) { if (IniLine != "") { // Si la section est la bonne if (IniLine == "[" + pSection + "]") { IniInSection = true; } // Si on sort de la section else if (IniLine.First() == '[' && IniLine.Last() == ']') { IniInSection = false; } // Si dans la section et que y'a la bonne clé else if (IniInSection && IniLine.Length > pKey.Length && IniLine.Substring(0, pKey.Length + 1) == pKey + "=") { return(IniLine.Substring(pKey.Length + 1)); } } } return(""); }
// =============================================================================================================================== // ==[FONCTION]=================================================================================================================== // Fonction : RenameKey // Description : Renomme une clé d'une section du fichier .ini // Argument(s) : string pSection = Nom de la section ou se trouve la clé // : string pKey = Nom de la clé à renommer // : string pNewName = Nouveau nom // Créateur(s) : BUSTOS Thibault(TheRake66) // Version : 1.0 // =============================================================================================================================== public void RenameKey(string pSection, string pKey, string pNewName) { string[] IniText; string IniConvert = ""; bool IniInSection = false; try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); } catch { return; } foreach (string IniLine in IniText) { if (IniLine != "") { // Si la section est la bonne if (IniLine == "[" + pSection + "]") { IniConvert += IniLine + "\n"; IniInSection = true; } // Si on sort de la section else if (IniLine.First() == '[' && IniLine.Last() == ']') { IniConvert += IniLine + "\n"; IniInSection = false; } // Si dans la section et que y'a la bonne clé else if (IniInSection && IniLine.Length > pKey.Length && IniLine.Substring(0, pKey.Length + 1) == pKey + "=") { IniConvert += pNewName + "=" + IniLine.Substring(pKey.Length + 1) + "\n"; } else { IniConvert += IniLine + "\n"; } } } try { File.WriteAllText(this.IniFile, IniConvert); } catch { } }
// =============================================================================================================================== // ==[FONCTION]=================================================================================================================== // Fonction : DeleteSection // Description : Supprime une section du fichier .ini // Argument(s) : string pSection = Nom de la section à supprimer // Créateur(s) : BUSTOS Thibault(TheRake66) // Version : 1.0 // =============================================================================================================================== public void DeleteSection(string pSection) { string[] IniText; string IniConvert = ""; bool IniInSection = false; try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); } catch { return; } foreach (string IniLine in IniText) { if (IniLine != "") { // Si la section est la bonne if (IniLine == "[" + pSection + "]") { IniInSection = true; } // Si on sort de la section else if (IniLine.First() == '[' && IniLine.Last() == ']') { IniConvert += IniLine + "\n"; IniInSection = false; } // Si dans la section et que y'a la bonne clé else if (IniInSection) { } else { IniConvert += IniLine + "\n"; } } } try { File.WriteAllText(this.IniFile, IniConvert); } catch { } }
public void SetValue(string group, string item, string value) { bool groupExists = false; foreach (IniLine line in Contents) { if (line is IniLineKV) { IniLineKV ilkv = line as IniLineKV; if (ilkv.GroupName.ToLower() == group.ToLower()) { groupExists = true; if (ilkv.ItemName.ToLower() == item.ToLower()) { if (ilkv.ItemValue != value) { //found it! Dirty = true; ilkv.ItemValue = value; ilkv.dirty = true; } return; } } } } //we didn't find it, have to add it. //if the group doesn't exist, we can just append this to the end of the file if (!groupExists) { Dirty = true; var line1 = new IniLine(""); line1.dirty = true; Contents.Add(line1); //whitespace var line2 = new IniLineGroup("[" + group + "]", group); line2.dirty = true; Contents.Add(line2); var line3 = new IniLineKV(item + "=" + value, group); Contents.Add(line3); } else { //otherwise we have to do a little legwork //scan for the last line of the target group int l = 0; int ll = 0; bool g = false; for (; l < Contents.Count; l++) { if (!g) //still searching for the appropriate group { if (Contents[l] is IniLineGroup) { if ((Contents[l] as IniLineGroup).GroupName == group) { //found the group g = true; continue; } } } else //we have a group, seeking to the end of it { if (Contents[l] is IniLineKV) { if ((Contents[l] as IniLineKV).GroupName != group) { //ll is now our target index. break out! break; } ll = l + 1; } } } Dirty = true; var line = new IniLineKV(item + "=" + value, group); line.dirty = true; Contents.Insert(ll, line); } }
// =============================================================================================================================== // ==[FONCTION]=================================================================================================================== // Fonction : WriteKey // Description : Écrit une clé d'une section du fichier .ini // Argument(s) : string pSection = Nom de la section ou se trouve la clé // : string pKey = Nom de la clé à écrire // : string pValue = Valeur de la clé // Créateur(s) : BUSTOS Thibault(TheRake66) // Version : 1.0 // =============================================================================================================================== public void WriteKey(string pSection, string pKey, string pValue) { string[] IniText; bool IniInSection = false; string IniConvert = ""; bool IniModif = false; bool IniFoundSection = false; // Créer le fichier si il existe pas try { if (!File.Exists(this.IniFile)) { FileStream CreateFile = File.Create(this.IniFile); CreateFile.Close(); } } catch { return; } try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); } catch { return; } foreach (string IniLine in IniText) { if (IniLine != "") { // Si la section est la bonne if (IniLine == "[" + pSection + "]") { IniConvert += IniLine + "\n"; IniInSection = true; IniFoundSection = true; } // Si on sort de la section else if (IniLine.First() == '[' && IniLine.Last() == ']') { // Si on sort de la section sans avoir ecrit if (IniInSection && !IniModif) { IniConvert += pKey + "=" + pValue + "\n"; IniModif = true; } IniConvert += IniLine + "\n"; IniInSection = false; } // Si dans la section et que y'a la bonne clé else if (IniInSection && IniLine.Length > pKey.Length && IniLine.Substring(0, pKey.Length + 1) == pKey + "=") { IniConvert += pKey + "=" + pValue + "\n"; IniModif = true; } else { IniConvert += IniLine + "\n"; } } } // Si la section n'existe pas if (!IniFoundSection) { IniConvert += "[" + pSection + "]\n"; } // Si la clé existe pas if (!IniModif) { IniConvert += pKey + "=" + pValue + "\n"; } try { File.WriteAllText(this.IniFile, IniConvert); } catch { } }