|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhile 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. BackgroundAn 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: .
[section1]
key1=1
key2=string1
.
.
.
Using the CodeThe attached IniFile.cs file contains all the code for the //Create a new IniFile object.
IniFile iniFile = new IniFile(@"c:\boot.ini");
Reading from an INI FileSimilar to the //Read an Int16 value from an INI file.
short value1 = iniFile.GetInt16("section1", "key1", 0);
//Read an Int32 value from an INI file.
int value2 = iniFile.GetInt32("section1", "key1", 0);
//Read a String from an INI file.
string value3 = iniFile.GetString("section1", "key2", "default value");
//Read an Double value from an INI file.
double value4 = iniFile.GetDouble("section1", "key3", 0.0);
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. The //Get the list of sections in the INI file.
string [] sectionNames = iniFile.GetSectionNames();
//Get the list of keys in the first section.
string [] keyNames = iniFile.GetKeyNames(sectionNames[0]);
Finally, the
//Get the key/value pairs in a section of the INI file as a Dictionary.
//Get the key/values in section1.
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, //Get the key/value pairs in a section of the INI file as a List.
//Get the key/values in section1.
List<KeyValuePair<string, string>> sectionValues;
sectionValues = iniFile.GetSectionValuesAsList("section1");
foreach (KeyValuePair<string, string> pair in sectionValues)
{
//Process the keys here...
}
Both of these methods share the same 32KB limit as Writing to an INI FileThe //Write a string to the iniFile
iniFile.WriteValue("section1", "key2", "Test value");
//Write a Int32 to the iniFile.
iniFile.WriteValue("section1", "key1", 128);
//Write a Double to the iniFile.
iniFile.WriteValue("section1", "key1", 42.8);
Deleting Data from an INI FileThe //Delete key2 from section1
iniFile.DeleteKey("section1", "key2");
//Delete section2 (including all keys)
iniFile.DeleteSection("section2");
History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||