Code:
using System.Runtime.InteropServices;
private class INI
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, uint nSize, string lpFileName);
public string FileName { get; private set; }
public INI(string file)
{
FileName = file;
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, FileName);
}
public string Read(string section, string key)
{
string str = null;
GetPrivateProfileString(section, key, "", str, 1024, FileName);
return str;
}
}
Code:
/* INI Class
* Coded by Shawak from Elitepvpers
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
public class INI
{
public string FileName { get; private set; }
private Dictionary<string, INIValueCollection> sections;
public INI(string filename)
{
FileName = filename;
sections = new Dictionary<string, INIValueCollection>();
parsing = false;
ParseFile(FileName);
}
public INIValueCollection this[string section]
{
get
{
if (sections.ContainsKey(section))
return sections[section];
else
{
var s = new INIValueCollection(this);
sections.Add(section, s);
return s;
}
}
}
private bool parsing;
public void ParseFile(string filename)
{
parsing = true;
using (var reader = new StreamReader(new FileStream(FileName, FileMode.OpenOrCreate), Encoding.UTF8))
{
string line = null;
string lastSection = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.StartsWith("#"))
continue;
if (line.StartsWith("[") && line.EndsWith("]"))
{
line = line.Substring(1, line.Length - 2);
lastSection = line.Trim();
}
else if(line.Contains("=") && !String.IsNullOrEmpty(lastSection))
{
var lineSplit = line.Split('=');
var value = lineSplit[1].Trim();
if(value.StartsWith("\"") && value.EndsWith("\""))
value = value.Substring(1, value.Length - 2);
this[lastSection][lineSplit[0].Trim()] = value;
}
}
}
parsing = false;
}
public void Save()
{
if (parsing)
return;
using (var writer = new StreamWriter(new FileStream(FileName, FileMode.Truncate), Encoding.UTF8))
{
foreach (var section in sections)
{
writer.WriteLine("[" + section.Key + "]");
foreach (var value in sections[section.Key].Values)
writer.WriteLine(value.Key + " = " + (value.Value is string ? "\"" : "") + value.Value + (value.Value is string ? "\"" : ""));
writer.WriteLine();
}
}
}
public class INIValue
{
public string Key { get; set; }
public object Value { get; set; }
}
public class INIValueCollection
{
public List<INIValue> Values { get; private set; }
public INI Owner { get; private set; }
public INIValueCollection(INI owner)
{
Owner = owner;
Values = new List<INIValue>();
}
public object this[string key]
{
get
{
var val = Values.FirstOrDefault(e => e.Key == key);
if (val != null)
return val.Value;
return null;
}
set
{
var val = Values.FirstOrDefault(e => e.Key == key);
if (val != null)
val.Value = value;
else
Values.Add(new INIValue() { Key = key, Value = value });
Owner.Save();
}
}
}
}