While it is no longer the suggested method for storing data by Microsoft, for legacy reasons many applications still need to read and write INI files. Win32 provides several methods for working with INI files, but these methods are not exposed by the .NET Framework. This article presents a simple class to allow a managed application to interact with INI files.
An INI file is a specially formatted text file containing one or more sections. Each section can have one or more settings (or keys). An example file is shown below:
Using the Code
The attached IniFile.cs file contains all the code for the
IniFile class. You create a new instance of this class by passing the path of the INI file to read and/or write to the constructor.
IniFile iniFile = new IniFile(@"c:\boot.ini");
Reading from an INI File
Similar to the
System.Data.DataReader.IDataReader interface, the
IniFile class provides several methods for reading different types of data from an INI file. Each of these methods takes the form "GetX", where X is the requested data type of the return value. For example, to read a
System.Int32 value from your INI file, you use the
short value1 = iniFile.GetInt16("section1", "key1", 0);
int value2 = iniFile.GetInt32("section1", "key2", 0);
string value3 = iniFile.GetString("section1", "key3", "default value");
double value4 = iniFile.GetDouble("section1", "key4", 0.0);
bool value5 = iniFile.GetBoolean(quot;section1", "key5", true);
As shown above, each of these methods takes three parameters. The first specifies the name of the section to read from. The second specifies the specific key to read from. The final parameter specifies the value to return if the section/key is not found. Please note that string values are limited to 32KB or less. Attempting to read a setting that is more than 32KB in length will result in the data being truncated to 32KB.
IniFile class also provides methods to enumerate the list of sections or keys in a file. Each of these methods returns an array of strings.
string  sectionNames = iniFile.GetSectionNames();
string  keyNames = iniFile.GetKeyNames(sectionNames);
IniFile class provides methods that allow a caller to read all of the keys in a given section at once. This functionality is provided by two functions:
GetSectionValues is the more convenient of the two. It returns all the key/value pairs in a section as a
Dictionary<string, string> object. This makes it very easy to get the value of several properties at one time.
Dictionary<string, string> sectionValues = iniFile.GetSectionValues("section1");
string value1 = sectionValues["key1"];
string value2 = sectionValues["key2"];
If a section contains more than one value with the same key,
GetSectionValues always returns the first one in the file. If you need to be able to get all the values in a section no matter what the key names are, you can use the
List<KeyValuePair<string, string>> sectionValues;
sectionValues = iniFile.GetSectionValuesAsList("section1");
foreach (KeyValuePair<string, string> pair in sectionValues)
Both of these methods share the same 32KB limit as
GetString. If your section can contain more than 32KB of data, read each setting separately.
Writing to an INI File
IniFile class also provides methods to write to an INI file. The various overloads of the
WriteValue method allow the caller to write strings, integers, or doubles to an INI file. Each of these functions takes three parameters. The first specifies the name of the section to write to. The second specifies the specific key to write to. The final parameter specifies the new value for the key. If the section and key already exist, the existing value is overwritten. If the specified section or key do not exist, they are automatically created.
iniFile.WriteValue("section1", "key2", "Test value");
iniFile.WriteValue("section1", "key1", 128);
iniFile.WriteValue("section1", "key1", 42.8);
iniFile.WriteValue("section1", "key1", true);
Deleting Data from an INI File
IniFile class provides methods to delete keys and sections from an INI file. Use
DeleteKey to delete a specific key and
DeleteSection to delete a complete section from an INI file.
- Corrected return type of GetInt16
- Added GetBoolean and new WriteValue method to write boolean values
- GetDouble no longer throws an exception if the text value of the key could not be parsed. It now returns the default value in this case.
- Fixed some spelling errors in comments. Added documentation about exceptions the constructor can throw.
- Added parameter checking
Added notes about the 32KB limitation for most methods
- Fixed XML comments
- 8/14/2007 - Initial version