C # using file streams to read and write INI files

Source: Internet
Author: User

Background The INI file was used as the configuration file of the program. I think this structure is simple and easy to configure. Then, you can use WindowsAPI to find a help Class Based on WindowsAPI encapsulation on the Internet. I thought I could use the file stream for operations. Later I found EasyConfig. EasyConfig is a pure C # Open-source INI file operating library, but it is inconvenient to use. It is just my personal feeling. So I transformed it into my favorite style. If you do not know the structure of the INI file, click "Baidu" and paste a sample file here. INI file example EasyConfig original EasyConfig after rewriting the code example first to see how to use her to read and write INI files after rewriting. Static void Main (string [] args) {ConfigFile configFile = new ConfigFile ("Test. ini "); // traverse the Config file foreach (var group in configFile. settingGroups) {Console. writeLine ("****************************"); Console. writeLine (group. key + ":"); Console. writeLine (); foreach (var value in group. value. settings) Console. writeLine ("{0 }={ 1} (Is Array? {2}), {3} ", value. key, value. value. rawValue, value. value. isArray, value. value. desp); Console. writeLine ();} // The read value is mainly read on a specific configuration item. The first is to locate the [Group], and the second is the specific item // use the generic read value, specify the default value var fullScreen = configFile ["Video"] ["Fullscreen"] When the read fails. as <int> (-1); // failed var bFullScreen = configFile ["Video"] ["Fullscreen"]. asBool (); // success var arrCanShoot = configFile ["Level1"] ["CanShoot"]. asArray <bool> (); // when reading, this item does not exist. a nonexistent group or item is automatically added. Add var noexists = configFile ["Video"] ["xxxxxxxxxxx"]. asString (); var noexists2 = configFile ["Video111111111"] ["xxxxxxxxxxx"]. asString (); // There are two methods for writing values. You can write an item directly to a group or locate an item in a group. The write value // The write value does not exist, the configFile ["Video"] is automatically created for the group or item. writeSetting ("NewName", "EasyConfig"); configFile ["Video"]. writeSetting ("NewName", "EasyConfig2.0"); configFile ["Video22222222"]. writeSetting ("c1", "1"); // The indexer returns Setting configFile ["Video3333"] ["UserName"]. setValue ("admin"); configFile ["Viedo4444"] ["Sex"]. setValue ("male", "female", "confidential"); // write value. configFile ["Video222"] does not exist. writeSetting ("NewName", "EasyConfig3.0"); Console. readKey (true); configFile. save ("TestConfig2.txt");} basically the effect you want is read and write based on ConfigFile ["GroupName"] ["Key. In addition, an encapsulation Class Based on WindowsAPI operations is attached. IniFileHelp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.