|
extensibility the three types of information in a standard ini file often did not suffice to store the information needed by the application. ive seen (and built) applications that rely on custom string formats to perform tasks beyond the ini file formats native capabilities. for example, its not uncommon to see ini files that contain strings that use separators to "squash" more values into an ini file: [testing] 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 edit—and maintain—but 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 and setcustominiattribute 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"?> <sections> <comment>company employees</comment> <section name="employees"> <item key="employee1" value="bob johnson" department="accounting" testedon="04/03/2001 8:23:14" score="85"/> <item key="employee2" value="susan fielding" department="sales" testedon="03/23/2001 15:41:48" score="92" /> </section> </sections> its much easier to discover the meaning of the data in the xml version. at a more complex level, although i havent implemented it in the sample code, you could add getcustominielement and setcustominielement 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 wheres my ini file? having a separate file for storing application initialization information is a good idea. the .net framework contains built-in methods for handling configuration data, but—as delivered—they arent 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, ill 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.
|
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
相关推荐
- 暂无文章

