PHP Code:
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace _modiX.Utilities
{
/// <summary>
/// Create a New INI file to store or load data
/// </summary>
public class IniFile
{
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);
/// <summary>
/// INI file Constructor.
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string iniPath)
{
this.path = iniPath;
}
/// <summary>
/// Write Data to the INI File
/// </summary>
/// <param name="section"></param>
/// Section name
/// <param name="key"></param>
/// Key Name
/// <param name="value"></param>
/// Value Name
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, this.path);
}
/// <summary>
/// Write all data to INI file.
/// </summary>
/// <param name="data">Selection-Key-Value - Collection.</param>
public void WriteAll(Dictionary<string, Dictionary<string, string>> data)
{
foreach (string selection in data.Keys)
{
foreach (string key in data[selection].Keys)
{
this.Write(selection, key, data[selection][key]);
}
}
}
/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
public string Read(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
return temp.ToString();
}
/// <summary>
/// Read all data from INI file.
/// </summary>
/// <returns>Selection-Key-Value - Collection.</returns>
public Dictionary<string, Dictionary<string, string>> ReadAll()
{
Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
/*
* Try to read the content of a simle INI file
*/
List<string> selections = this.getSelections();
foreach (string selection in selections)
{
/*
* Get the keys
*/
List<string> keys = this.getKeys(selection);
foreach (string key in keys)
{
/*
* Now store the content
*/
if (!result[selection].ContainsKey(key))
{
result[selection].Add(key, "");
}
result[selection][key] = this.Read(selection, key);
}
}
return result;
}
/// <summary>
/// Shows the Path.
/// </summary>
/// <returns>Path to INI file.</returns>
public override string ToString()
{
return this.path;
}
private List<string> getKeys(string selection)
{
StringBuilder temp = new StringBuilder(32768);
GetPrivateProfileString(selection, null, null, temp, 32768, this.path);
List<string> result = new List<string>(temp.ToString().Split('\0'));
result.RemoveRange(result.Count - 2, 2);
return result;
}
private List<string> getSelections()
{
StringBuilder temp = new StringBuilder(65536);
GetPrivateProfileString(null, null, null, temp, 65536, this.path);
List<string> result = new List<string>(temp.ToString().Split('\0'));
result.RemoveRange(result.Count - 2, 2);
return result;
}
}
}
My little INI file class offers even more methods for a faster whole read/write shit.