This project has moved and is read-only. For the latest updates, please go here.


NuGet Package ID: CodeBits.IniFile

The IniFile class can open, modify and save .INI files.

Loading an .INI file

An .INI file can be loaded using one of the IniFile constructors or a static factory method called Load.
//Load .INI file from file path
var ini = new IniFile(@"C:\Windows\System.ini");

//Load .INI file from stream
var stream = new FileStream(@"C:\Windows\System.ini", FileMode.Open,
var ini = new IniFile(stream);

//Load .INI file from TextReader-derived classes
StreamReader reader = File.OpenText(@"C:\Windows\System.ini");
var ini = new IniFile(reader);

//To load a string containing the .INI file contents, use the Load static method
string iniFileContent = File.ReadAllText(@"C:\Windows\System.ini");
IniFile ini = IniFile.Load(iniFileContent);
You can also create an empty IniFile instance using the parameterless constructor:
var ini = new IniFile();

Saving an .INI file

The IniFile class provides several overloads to a method called SaveTo that allows you to save the contents of the IniFile class to a file, stream or TextWriter.
Additionally, you can get the entire contents as a string by calling the ToString method.

Note: The IniFile class does not preserve comments and formatting when loading INI data. Hence, when saving data loaded from INI data, all the comments and formatting will be lost.

Reading and modifying INI data

The IniFile class is a collection of IniFile.Section objects. Each IniFile.Section represents a section in the INI data.
Each IniFile.Section class is a collection of IniFile.Property objects, which represent the INI properties (key-value pairs).
Adding, deleting and modifying sections and properties in the IniFile instance is equivalent to modifying the respective collections.

var ini = new IniFile(@"C:\Windows\System.ini");

//Print all sections and their corresponding properties
foreach (IniFile.Section section in ini)
    Console.WriteLine("Section: {0}", section.Name);
    foreach (IniFile.Property property in section)
        Console.WriteLine("{0} = {1}", property.Key, property.Value);

//Adding a new section
IniFile.Section newSection = new IniFile.Section("New Section");

//Adding properties to a section
newSection.Add("Property1", "Value1");

//Finding a section
IniFile.Section someSection = ini["Some Section"];
if (someSection != null)
    Console.WriteLine("Found section called 'Some Section'");

.INI Load Options

All IniFile constructors and the static Load method have an optional parameter of type IniLoadOptions that contains options for loading INI data.

Property Description Default
CaseSensitive Indicates whether the sections and properties in the INI data are case-sensitive. False
DetectEncoding Indicates whether to detect the character encoding of the loaded INI data. If True, then this setting overrides the value specified by the Encoding setting. False
Encoding The expected character encoding of the INI data. UTF8

//Load an INI file and attempt to detect it's character encoding
var ini = new IniFile(@"C:\Windows\System.ini", new IniLoadSetting { DetectEncoding = true });

//Load INI content with ASCII encoding
IniFile ini = IniFile.Load(@"C:\Windows\System.ini", new IniLoadSetting { Encoding = Encoding.ASCII });

Last edited Mar 15, 2013 at 10:50 PM by jeevanjj, version 5


No comments yet.