Skip to content

Instantly share code, notes, and snippets.

@thennequin
Last active July 11, 2018 14:34
Show Gist options
  • Save thennequin/3c57cd20f5a19493fcd58f5393d1a08c to your computer and use it in GitHub Desktop.
Save thennequin/3c57cd20f5a19493fcd58f5393d1a08c to your computer and use it in GitHub Desktop.
IniFile.cs
/*
Usage:
struct MyStruct
{
public string MyString { get; set; } = "this is my value";
public int MyInt { get; set; } = 42;
public bool MyBool { get; set; } = true;
}
void main()
{
IniFile oIni = new IniFile("config.ini");
MyStruct oMyStruct = new MyStruct();
//Read content of config.ini to oMyStruct
oIni.ReadObject(oMyStruct);
oMyStruct.MyBool = false;
//Save content of oMyStruct to config.ini
oIni.WriteObject(oMyStruct);
/*Content of config.ini will be:
[MyStruct]
MyString=this is my value
MyInt=42
MyBool=false
*/
}*/
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;
namespace IniFile
{
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);
/// <summary>
/// INIFile Constructor.
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string INIPath)
{
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 IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
const string c_sNULLString = @"<<<<<<<<<<<<<<<<<<<<<<NULL>>>>>>>>>>>>>>>>>>>>>>>>";
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, c_sNULLString, temp,
255, this.path);
string sValue = temp.ToString();
if (sValue == c_sNULLString)
return null;
return sValue;
}
public void ReadObject(object obj)
{
if (obj == null)
return;
System.Type oType = obj.GetType();
PropertyInfo[] properties = oType.GetProperties();
if (properties == null)
return;
foreach(PropertyInfo property in properties)
{
string sValue = IniReadValue(oType.Name, property.Name);
if (sValue == null)
continue;
object value = null;
try
{
if (property.PropertyType == typeof(string))
{
value = sValue;
}
else if (property.PropertyType == typeof(int))
{
value = int.Parse(sValue);
}
else if (property.PropertyType == typeof(bool))
{
value = bool.Parse(sValue);
}
else
{
throw new System.Exception(string.Format("Not supported type '{0}' for property '{1}'", property.PropertyType.Name, property.Name));
}
property.SetValue(obj, value);
}
catch { }
}
}
public void WriteObject(object obj)
{
if (obj == null)
return;
System.Type oType = obj.GetType();
PropertyInfo[] properties = oType.GetProperties();
if (properties == null)
return;
foreach (PropertyInfo property in properties)
{
IniWriteValue(oType.Name, property.Name, property.GetValue(obj).ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment