Browse DevX
Sign up for e-mail newsletters from DevX


Upgrade Your INI Files to XML with .NET : Page 3

As you upgrade your .NET applications, upgrade those legacy INI files as well, and save yourself some headaches.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Updating individual values is similar to retrieving them. You provide a section name, a key name, and the new value. The class uses the GetItem method to locate the appropriate <item> element, and then updates that item's value attribute with the specified new value. The Windows API function WritePrivateProfileString creates new sections and items if you call it with a section or key name that doesn't already exist.

Although it's not good object-oriented design, for consistency the IniFileReader class acts identically, meaning that you can create a new section simply by passing a nonexistent section name. To update a section name, key name, or value for existing sections or items, select the section and item you want to update, enter the new values in the appropriate fields, and then click the Update button to apply the changes. To create a new section or key on the sample form, first click the New Section or New Key button to clear the current selections, and then enter the new section or key name and click the Update button to apply your changes.

To delete a section using the API, you pass the section name and a null key value to the WritePrivateProfileString function—and you do the same with the IniFileReader class, except that you use the SetIniValue method. For example, the following code would delete the section named section1.

SetIniValue("section1", null, null);

Similarly, to delete an item within a section, you pass the section name, the key name, and a null value for the value argument. The following code would delete the item with the key key1 in the section named section1.

SetIniValue("section1", "key1", null);

On the sample form (see Figure 1), you can delete a section or value by selecting the appropriate item in either the section or item list, and then pressing the Delete key.

Thanks for your registration, follow us on our social networks to keep up-to-date