The three types of information in a standard INI file often did not suffice to store the information needed by the application. I've seen (and built) applications that rely on custom string formats to perform tasks beyond the INI file format's native capabilities. For example, it's not uncommon to see INI files that contain strings that use separators to "squash" more values into an INI file:
emp1=Bob Johnson|Accounting|04/03/2001 8:23:14|85
emp2=Susan Fielding|Sales|03/23/2001 15:41:48|92
Not only are such files difficult to read and editand maintainbut they also require custom code to retrieve the item values and assign them to variables within the program. The data in such files is unusable by other programs without recreating the code to retrieve the data. In contrast, you can add new items to an XML-formatted INI file with few problems. At the most simplistic level, you can use the GetCustomIniAttribute
methods to read, write, and delete custom strings, stored as attributes within the <item>
elements. For example, the following XML listing shows the same data shown in the preceding INI file added as custom attributes.
<?xml version="1.0" encoding="UTF-8"?>
<item key="employee1" value="Bob Johnson"
It's much easier to discover the meaning
of the data in the XML version.
At a more complex level, although I haven't implemented it in the sample code, you could add GetCustomIniElement
methods to add custom child elements and values to the <item>
elements. These methods would be overloaded to accept an element name and value, an XmlElement
instance, or an XmlDocumentFragment
instance, so you could make the file as complicated as necessary. The Extensibility button on the sample form contains code that shows how to use the extensibility methods.
Beyond the built-in extensibility methods, you can, of course, subclass the IniFileReader
and override or add methods to do anything you like.
Dealing with Comments
You use the SetIniComments
methods to add and retrieve comments. The GetIniComments
method returns a StringCollection
containing the text of comments at either the file or section level. The SetIniComments
method accepts a StringCollection
containing a list of comments you want to add at either the file or section level. While this implementation is very crude, and could be greatly improvedfor example, you could extend the class to attach comments directly to individual itemsit's already an improvement over standard INI files, which provide no way to create or remove comments automatically. You can also add XML-formatted comments manually or use the DOM directly to add comments to an XML-formatted INI file.
Where's My INI File?
For straightforward (unextended) files, you can "get the original INI file back." In some cases, you will want to read and modify the INI file with a .NET application, but you still need access to the file in its original format usable by pre-.NET Windows applications. An XSLT transform performs the work of turning the file back into a standard INI file. You initiate the transform using the SaveAsIniFile
method. However, extensibility comes at a price. If you make custom
modifications to the file using the writeCustomIniAttribute
method, those changes will not
appear in the results of the transform. As there's little reason to translate the files back to standard INI format, that seems reasonable.
Having a separate file for storing application initialization information is a good idea. The .NET framework contains built-in methods for handling configuration data, butas deliveredthey aren't suitable for complex information structures. As you migrate existing applications into .NET, the INIFileReader class described in this article lets you use and even extend your existing INI files. Nonetheless, .NET configuration files have some advantages even over these custom external XML-formatted initialization files. In a future article, I'll show you how to achieve the same level of functionality using a custom configuration handler, which will let you place configuration data directly into your machine.config
or application configuration file.