Feedback

INI – Dateien lesen und schreiben

Sprache: C#

Mit dieser Klasse kann sehr einfach lesend und schreibend auf INI – Dateien zugegriffen werden.
public class IniFile
  {
    public string path;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
      string key,string val,string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
      string key,string def, StringBuilder retVal,
      int size,string filePath);
   
    public IniFile(string INIPath)
    {
      path = INIPath;
    }
    
    public void IniWriteValue(string Section,string Key,string Value)
    {
      WritePrivateProfileString(Section,Key,Value,this.path);
    }
   
    public string IniReadValue(string Section,string Key)
    {
      StringBuilder temp = new StringBuilder(255);
      int i = GetPrivateProfileString(Section,Key,"",temp,255, this.path);
      return temp.ToString();
    }
  }
public class IniFile
  {
    public string path;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
      string key,string val,string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
      string key,string def, StringBuilder retVal,
      int size,string filePath);
   
    public IniFile(string INIPath)
    {
      path = INIPath;
    }
    
    public void IniWriteValue(string Section,string Key,string Value)
    {
      WritePrivateProfileString(Section,Key,Value,this.path);
    }
   
    public string IniReadValue(string Section,string Key)
    {
      StringBuilder temp = new StringBuilder(255);
      int i = GetPrivateProfileString(Section,Key,"",temp,255, this.path);
      return temp.ToString();
    }
  }

1 Kommentar

  1. I modded your Snippet a little:
    [code]using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace JB.IO
    {
    // Source: http://dotnet-snippets.de/dns/ini—dateien-lesen-und-schreiben-SID60.aspx
    // Changed by JB: http://jbquerier.blogspot.com
    ///

    /// Klasse zum Schreiben von Ini-Dateien.
    ///

    public class IniFile
    {
    // JB: accessor changed.
    ///

    /// Pfad der Ini-Datei.
    ///

    private string path;

    [DllImport(„kernel32“)]
    private static extern long WritePrivateProfileString(string section,
    string key, string val, string filePath);

    [DllImport(„kernel32“)]
    private static extern int GetPrivateProfileString(string section,
    string key, string def, StringBuilder retVal,
    int size, string filePath);

    ///

    /// Konstruktor
    ///

    /// Pfad zur Ini-Datei
    public IniFile(string INIPath)
    {
    path = INIPath;
    }

    // Part by JB
    ///

    /// Sektion der Ini-Datei
    ///

    /// Name der Sektion
    /// Sektion
    public Section this[string Section]
    {
    get { return new Section(Section, this); }
    }

    // Part by JB
    ///

    /// Sektion-Struktur
    ///

    public class Section
    {
    private string Name;
    private IniFile File;

    ///

    /// Konstruktor für die Sektion
    ///

    /// Name der Sektion
    /// Verweis auf die Ini-Datei
    public Section(string name, IniFile file)
    {
    Name=name;
    File = file;
    }

    ///

    /// Schreibt oder ermittelt einen Ini-Wert.
    ///

    /// Bezeichner des Wertes
    /// String des Wertes
    public string this[string Key]
    {
    get { return File.IniReadValue(Name, Key); }
    set { if (value != this[Key]) File.IniWriteValue(Name, Key, value); }
    }
    }

    // JB: accessor changed.
    ///

    /// Schreibt einen Ini-Wert
    ///

    /// Name der Sektion
    /// Bezeichner des Wertes
    /// Wert
    protected void IniWriteValue(string Section, string Key, string Value)
    {
    WritePrivateProfileString(Section, Key, Value, this.path);
    }

    // JB: accessor changed.
    ///

    /// Ermittelt einen Ini-Wert
    ///

    /// Name der Sektion
    /// Bezeichner des Wertes
    /// Wert
    protected string IniReadValue(string Section, string Key)
    {
    StringBuilder temp = new StringBuilder(255);
    int i = GetPrivateProfileString(Section, Key, „“, temp, 255, this.path);
    return temp.ToString();
    }
    }
    }

    // Usage-Sample:
    public partial class MainWindow : Window
    {
    private JB.IO.IniFile Settings;

    public MainWindow()
    {
    InitializeComponent();
    Settings = new JB.IO.IniFile(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @“Demo.ini“);
    }

    private void IniWrite_Click(object sender, RoutedEventArgs e)
    {
    Settings[„DemoSection“][„DemoCaption“] = „DemoValue“;
    }

    private void IniRead_Click(object sender, RoutedEventArgs e)
    {
    MessageBox.Show(Settings[„DemoSection“][„DemoCaption“]);
    }
    }
    [/code]