[C#] INI-Files lesen und schreiben

using System.Text;
using System.Runtime.InteropServices;

/// <summary>
/// freeware helper class for using INI-files
/// (W) 2012 by admin of codezentrale.de
///
/// usage:
///
/// IniFile _ini = new IniFile("c:\\testini.ini");
/// if (_ini != null)
/// {
///     string r = _ini.IniReadValue("section", "key", "default");
///     
///     if (_ini.IniWriteValue("section", "key", "value"))
///     {
///         ...
///     }
/// }
/// </summary>
public class IniFile
{
    private string _INIfile = string.Empty;

    [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

    [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);

    /// <summary>
    /// current INI-file
    /// </summary>
    public string IniFileName
    {
        get { return _INIfile; }
    }

    /// <summary>
    /// INIFile constructor
    /// </summary>
    /// <PARAM name="INIPath">INI file</PARAM>
    public IniFile(string INIfile)
    {
        _INIfile = INIfile;
    }
    /// <summary>
    /// write data to the INI-file
    /// </summary>
    /// <param name="section">Section name</PARAM>
    /// <param name="key">Key Name</PARAM>
    /// <param name="value">Value Name</PARAM>
    /// <returns>true if success</returns>
    public bool IniWriteValue(string section, string key, string val)
    {
        return WritePrivateProfileString(section, key, val, _INIfile);
    }

    /// <summary>
    /// read data value from the INI-file
    /// </summary>
    /// <param name="section"section name</PARAM>
    /// <param name="key">key name</PARAM>
    /// <param name="def">default value on error</param>
    /// <returns>Value or def on error</returns>
    public string IniReadValue(string section, string key, string def)
    {
        StringBuilder sb = new StringBuilder(255);

        int iCharsRead = GetPrivateProfileString(section, key, def, sb, (uint)sb.Capacity, _INIfile);
        
        return sb.ToString();
    }
}