Giter Club home page Giter Club logo

ini-parser-standard's Introduction

INI File Parser

A .NET Standard library for reading/writing INI data from IO streams, file streams, and strings.

Also implements merging operations, both for complete ini files, sections, or even just a subset of the keys contained by the files.

Install it with NuGet: https://www.nuget.org/packages/IniFileParserStandard/

Getting Started

All code examples expect the following using clauses:

using IniParser;
using IniParser.Model;

INI data is stored in nested dictionaries, so accessing the value associated to a key in a section is straightforward. Load the data using one of the provided methods.

var parser = new IniFileParser();
IniData data = parser.ReadFile("Configuration.ini");

Retrieve the value for a key inside of a named section. Values are always retrieved as strings.

string useFullScreenStr = data["UI"]["fullscreen"];
// useFullScreenStr contains "true"
bool useFullScreen = bool.Parse(useFullScreenStr);

Modify the value in the dictionary, not the value retrieved, and save to a new file or overwrite.

data["UI"]["fullscreen"] = "true";
parser.WriteFile("Configuration.ini", data);

Merging ini files

Merging ini files is a one-method operation:

   var parser = new IniStringParser();

   IniData config = parser.Parse(File.ReadAllText("global_config.ini"));
   IniData user_config = parser.Parse(File.ReadAllText("user_config.ini"));
   config.Merge(user_config);

   // config now contains that data from both ini files, and the values of
   // the keys and sections are overwritten with the values of the keys and
   // sections that also existed in the user config file

Keep in mind that you can merge individual sections if you like:

config["user_settings"].Merge(user_config["user_settings"]);

Comments

The library allows modifying the comments from an ini file. However note than writing the file back to disk, the comments will be rearranged so comments are written before the element they refer to.

To query, add or remove comments, access the property Comments available both in SectionData and KeyData models.

var listOfCommentsForSection = config.["user_settings"].Comments;
var listOfCommentsForKey = config["user_settings"].GetKeyData("resolution").Comments;

ini-parser-standard's People

Contributors

lukazh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.