SoftCircuits.IniFileParser 2.1.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package SoftCircuits.IniFileParser --version 2.1.0                
NuGet\Install-Package SoftCircuits.IniFileParser -Version 2.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SoftCircuits.IniFileParser" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SoftCircuits.IniFileParser --version 2.1.0                
#r "nuget: SoftCircuits.IniFileParser, 2.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SoftCircuits.IniFileParser as a Cake Addin
#addin nuget:?package=SoftCircuits.IniFileParser&version=2.1.0

// Install SoftCircuits.IniFileParser as a Cake Tool
#tool nuget:?package=SoftCircuits.IniFileParser&version=2.1.0                

IniFileParser

NuGet version (SoftCircuits.IniFileParser)

Install-Package SoftCircuits.IniFileParser

IniFile is a lightweight .NET class library that makes it easy to read and write INI files. It provide direct support for string, int, double and bool setting. It can return all of the sections in an INI file, and also return all the settings within a particular INI-file section.

Writing Settings

To write an INI file, create an instance of the IniFile class and call the SetSetting() method to set each setting. This method is overloaded to accept string, int, double and bool value types. Then call the Save() method to write the settings to a file.

// Write values
IniFile file = new IniFile();

file.SetSetting(IniFile.DefaultSectionName, "Name", "Bob Smith");
file.SetSetting(IniFile.DefaultSectionName, "Age", 34);
file.SetSetting(IniFile.DefaultSectionName, "Rating", 123.45);
file.SetSetting(IniFile.DefaultSectionName, "Active", true);

file.Save(path);
Reading Settings

To read an INI file, create an instance of the IniFile class and call the Load() method to read the settings from a file. Then call the GetSetting() method to get each setting.

The GetSetting() method accepts a default value parameter. The method returns the value of this parameter if the setting was not found or could not be converted to the specified type. The GetSetting() method is overloaded to accept default values of type string, int, double and bool. The default value parameter determines the return type.

// Read values
IniFile file = new IniFile();

file.Load(path);

string name = file.GetSetting(IniFile.DefaultSectionName, "Name", string.Empty));
int age = file.GetSetting(IniFile.DefaultSectionName, "Age", 0));
double rating = file.GetSetting(IniFile.DefaultSectionName, "Rating", 0.0));
bool active = file.GetSetting(IniFile.DefaultSectionName, "Active", false));

If any settings are found that are not under a section header, they will be added to the IniFile.DefaultSectionName section.

Reading All Sections in the File

Use the GetSections() method to retrieve all the sections in the file.

IEnumerable<string> sections = file.GetSections();
Reading All Settings in a Section

Use the GetSectionSettings() method to retrieve all the settings in a section.

IEnumerable<IniSetting> settings = file.GetSectionSettings(IniFile.DefaultSectionName);
Comments and Empty Lines

By default, any line with a semicolon (;) as the first non-space character is assumed to be a comment. The comment character can be changed by setting the CommentCharacter property.

In addition, any comments found when reading an INI file are stored in the Comments collection. And any comments in this collection will be written when saving an INI file. This makes it easy to add comments to INI files you create, or to maintain comments in INI files you modify. (Note, however, that all comments are written to the start of the INI file regardless of where those comments might have been when read.)

Empty lines are also ignored.

Custom Boolean Handling

By default, the bool version of the GetSetting() method understands the words "true", "false", "yes", "no", "on", "off", "1" and "0", and will convert those words to the corresponding bool value. The comparison is not case-sensitive. In addition, any string value that can be interpreted as an integer value will considered true if that integer value is non-zero, or false if that integer value is zero.

However, you can override these settings by passing an instance of the BoolOptions class to the IniFile constructor. The following example creates an instance of the BoolOptions class, sets the string comparer to use when comparing Boolean words, replaces the default Boolean words with a new list, specifies that strings that can be interpreted as integers should be translated to bool values, and passes the object to the IniFile constructor.

BoolOptions options = new BoolOptions(StringComparer.CurrentCultureIgnoreCase);
options.SetBoolWords(new[] {
    new BoolWord("sure", true),
    new BoolWord("okay", true),
    new BoolWord("yeppers", true),
    new BoolWord("nope", false),
    new BoolWord("nah", false),
    new BoolWord("nopers", false),
});
options.NonZeroNumbersAreTrue = true;

IniFile file = new IniFile(StringComparer.CurrentCultureIgnoreCase, options);

NOTE: The SetBoolWords() method will search the word list to find the first word with a true value and the first word with a false value, and these words will then be used by the SetSetting() method to write bool values. In the example above, any bool values would be written as "sure" or "nope". Keep this in mind if you are writing INI files that may be read by other programs, as INI parsers expecting bool values to be "true" or "false" would be unable to correctly interpret such a file. If the list of words passed to SetBoolWords() does not include any words with a true value, or does not include any words with a false value, an exception is thrown.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SoftCircuits.IniFileParser:

Package Downloads
revealnext.CommonLibs.Common

Part of REGEN Platform 13

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.0 1,275 3/24/2024
2.1.0 14,170 12/12/2021
2.0.1 2,920 4/30/2021
2.0.0 387 2/21/2021
1.0.11 951 5/15/2020
1.0.10 483 3/12/2020
1.0.9 512 3/1/2020
1.0.8 559 2/15/2020
1.0.7 482 2/11/2020
1.0.6 636 7/22/2019
1.0.5 472 7/18/2019
1.0.4 490 7/15/2019
1.0.3 493 7/14/2019
1.0.2 503 7/14/2019
1.0.1 476 7/7/2019
1.0.0 516 7/7/2019

Added async methods; Now also targeting .NET 6.0.