In diesem Beispiel wird das Auslesen einer INI-Datei gezeigt. Hier erst mal der Inhalt der INI-Datei:
1 2 3 | [Option] Farbe=blau Name=Hans |
Der Quellcode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System.Runtime.InteropServices; using System.IO; [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); public string GetString(string section, string key) { const int size = 255; string file = Environment.CurrentDirectory + "\\test.INI"; string def = ""; StringBuilder sb = new StringBuilder(size); GetPrivateProfileString(section, key, def, sb, size, file); return sb.ToString(); } |
Auslesen des Schlüssels “Farbe” aus der Sektion “Options”:
1 2 | string[] args; args = new string[] {GetString("Options", "Farbe")}; |
Tags: ini