INI Tools Sharp is a little project to manipulate and output INI files. You can find it on the NuGet Package Manager or the GitHub Package Manager.
The package is inspired by that of System.Text.Json, where you can access two primary static methods, INISerialiser.Deserialise
and INISerialiser.Serialise
to convert generic classes into INI format and back.
A class represents a section by giving it a IniSectionAttribute
and a name.
Test class to work with:
public class Section1
{
public bool Value1 { get; set; } = false;
public int Value2 { get; set; } = -1;
}
public class SomeSettings
{
[IniSection("SectionName")]
public Section1 Section { get; set; } = new Section1();
}
You can then serialise it into a INI file format:
var text = IniSerialiser.Serialise(new SomeSettings());
This will output text as follows:
[SectionName]
Value1=False
Value2=-1
The same text can be deserialised back into the SomeSettings
object.
You can also use simple list types as follows:
public class Section1
{
public List<int> Values { get; set; } = new List<int>()
{
5,
123,
-1
}
}
public class SomeSettings
{
[IniSection("SectionName")]
public Section1 Section { get; set; } = new Section1();
}
You can then serialise it into a INI file format:
var text = IniSerialiser.Serialise(new SomeSettings());
This will output text as follows:
[SectionName]
Values=[5,123,-1]
The same text can be deserialised back into the SomeSettings
object.