Here's another C# implementation I made for one of my previous projects:
EDIT: Interesting, wonder how you did the "remove keys". Manual parsing or Kernel32?
EDIT2: I'll just download the source and see for myself. The description seems quite amazing.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
public class IniFile
{
public string Path { get; set; }
[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 path, bool forceLoad)
{
if (!File.Exists(this.Path))
{
throw new FileNotFoundException();
}
this.Path = path;
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, this.Path);
}
public string Read(string section, string key)
{
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(section, key, "", sb, 255, this.Path);
string result = sb.ToString();
if (result == string.Empty)
{
throw new Exception("Cannot read field \"" + key + "\" at section \"" + section + "\".");
}
else
{
return result;
}
}
}