Mit einer INI-Library können Daten in einem Programm einfach in einer Datei zur Konfiguration gelesen oder gespeichert werden. Dazu erstellt man einfach eine neue Klasse mit dem Namen INI mit dem unten Aufgeführten Inhalt. Der Kommentar über dem Konstruktor zeigt, wie man ein neues Objekt der Klasse erzeugt. Der Aufruf der Entsprechenden Methode führt dann zum Ziel.
Quellcode
using System.Runtime.InteropServices; using System.Text; class INI { public string iniPath; [DllImport("kernel32")] static extern long WritePrivateProfileString(string section, string key, string Value, string filePath); [DllImport("kernel32")] static extern int GetPrivateProfileString(string section, string key, string Default, StringBuilder RetVal, int Size, string FilePath); public INI(string iniPath) { this.iniPath = iniPath; } public string ReadValue(string section, string key) { StringBuilder RetVal = new StringBuilder(255); GetPrivateProfileString(section != null ? section : iniPath, key, "", RetVal, 255, iniPath); return RetVal.ToString(); } public void WriteValue(string section, string key, string Value) { WritePrivateProfileString(section != null ? section : iniPath, key, Value, iniPath); } public void Deletekey(string section, string key) { WriteValue(section != null ? section : iniPath, key, null); } public void Deletesection(string section) { WriteValue(section, null, null); } public bool KeyExists(string section, string key) { return ReadValue(section, key).Length > 0 ? true : false; } public void Backup(string path) { System.IO.File.Copy(iniPath, path); } public void DeleteINI() { System.IO.File.Delete(iniPath); } }