ConfigTextFile 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ConfigTextFile --version 1.4.0                
NuGet\Install-Package ConfigTextFile -Version 1.4.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="ConfigTextFile" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfigTextFile --version 1.4.0                
#r "nuget: ConfigTextFile, 1.4.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 ConfigTextFile as a Cake Addin
#addin nuget:?package=ConfigTextFile&version=1.4.0

// Install ConfigTextFile as a Cake Tool
#tool nuget:?package=ConfigTextFile&version=1.4.0                

ConfigTextFile

A .NET library which allows you to load plain-text configuration files.

Overview

There are three different kinds of elements in this config file; Sections, Comments, and Key/Value pairs. Sections are denoted by a name and braces. Comments are preceded by #. Key/Value pairs are delimited with the equals sign, and Values can either be a single string, or an array of strings.

Usage

The ConfigFile class represents a single loaded file. To load one, use the static method TryLoadFile. It accepts either a stream, or string and Encoding. Once it's loaded, you can either use ConfigFile.GetElement("key") to retrieve parts of the file, or use the IConfiguration interface i.e. ConfigFile.GetSection("key") You can also use the ConfigFile.Elements Dictionary, but it's inconvenient for reading arrays.

LoadResult result = ConfigFile.TryLoadFile("MyFile.cfg", Encoding.UTF8);
if(result.Success)
{
	ConfigFile file = result.ConfigTextFile;

	// We can interpret it as an IConfigurationSection by using GetSection
	string myString = file.GetSection("SomeSection:SomeKey").Value;

	// We can just directly get the string this way. This is less verbose, but throws exceptions when keys are not found or are not a single string
	myString = file["SomeSection:SomeKey"];

	// And finally this lets us use the IConfigElement interface
	IConfigElement singleString = file.GetElement("SomeSection:SomeKey");

	// Throws an exception if the above key was not found
	singleString.ThrowIfInvalid();

	// This returns an empty string if it's not a single string
	myString = singleString.Value;

	IConfigElement array = file.Elements["SomeArray"];
	// We can iterate over each string in the array by doing this
	foreach(IConfigElement elem = array.Elements.Values)
	{
		Console.WriteLine("Array element: " + elem.Value);
	}

	// Can get the sections, and loop over everything they have
	IConfigElement section = file.Elements["SomeSection"];
	foreach(IConfigElement child = section.Elements.Values)
	{
		// The Type member denotes what this is; it's either String, Array, or Section. Types of Invalid are only returned by GetSection or GetElement, so we don't need to worry about that here.
		switch(child.Type)
		{
			case ConfigElementType.String:
				// We can get just use child.Value to get the string in this case
				break;
			case ConfigElementType.Array:
				// We can loop over this child's elements and print all the strings, say
				break;
			case ConfigElementType.Section:
				// We could loop over this child's elements
				break;
		}
	}

	// If we need to get at the root section, we can do that too
	ConfigSectionElement root = file.Root;
}
else
{
	Console.WriteLine("Failed to load file: " + result.ErrMsg);
}

Examples

Strings, Arrays, Comments

Here is a config file with some keys, strings, and string arrays, and descriptive comments.

# Syntax is simple
Key=Hello World!

# Keys can have spaces, and so can values. Quotes aren't even required!
Key With Spaces= Hello again, World!

# Strings can span multiple lines, but only if they are quoted. You can use "double quotes", 'single quotes', or `backticks` to quote strings
MultiLine="This is a multiline string
It spans many lines"

# Keys can have the = sign in them if you quote them
"Keys = Can be Quoted" = "And so can the values"

# Quotes work with both Keys and Values! You can use different quotes within strings
'Single"Quoted"Key'=`This string has backticks, so we don't get screwed by "different" quotes`

# Quotes only have an effect if they are the very first thing in the string. You can use all kinds of quotes all you want so long as they're not the first character of the string.
Doesn't Cause Problems=We're using "quotes" `just` fine!

# String Arrays are defined as per below.
"My Array"=[StringOne, String two, "String Three", "Multiline
within an array"]

# You don't need an equals sign when defining a array, it's optional (but may be easier to read). The below still works.
"Interpreted as an Array" [Element 1, Element 2]

Sections

Sections are defined by {braces}. Using sections will cause the resultant paths of the config elements to be constructed the key of each section, and finally the key of the value. In the case of arrays, the key of the value is followed by the array index, to represent a specific string in the array.

global{
	# When loaded, this becomes global:ValueOne
	ValueOne = Hello Scope!

	nested{
		# You can nest sections, too. This will become global:nested:ValueTwo
		ValueTwo = Deeper Scope!
	}
	
	# Each of these strings gets a separate path. They are, in order: global:Collection:0, global:Array:1, global:Array:2
	Array=[ValueOne, ValueTwo, ValueThree]
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ConfigTextFile:

Package Downloads
ConfigTextFile.ConfigurationSource

A ConfigTextFile configuration provider for Microsoft.Extensions.Configuration

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.12.0 815 11/15/2021 1.12.0 is deprecated because it is no longer maintained.
1.11.4 719 3/23/2021
1.11.3 478 3/17/2021
1.11.2 530 3/9/2021
1.11.1 508 1/9/2021
1.11.0 547 1/8/2021
1.10.0 560 12/22/2020
1.9.1 615 12/6/2020
1.9.0 400 12/2/2020
1.8.3 612 11/18/2020
1.8.2 454 11/9/2020
1.8.1 527 11/9/2020 1.8.1 is deprecated because it has critical bugs.
1.8.0 411 11/5/2020
1.7.1 447 10/3/2020
1.7.0 442 10/1/2020
1.6.2 442 8/27/2020
1.6.1 454 8/26/2020
1.6.0 518 7/29/2020
1.5.0 535 7/28/2020
1.4.0 448 6/26/2020
1.3.0 505 6/6/2020
1.2.0 473 5/21/2020
1.1.0 520 5/4/2020
1.0.0 471 4/21/2020

- When trying to get a string value by using array indexers e.g. ConfigSectionElement["key"], this now throws an exception rather than returning null, if the key does not resolve to a ConfigStringElement. Prefer array indexers if you want exceptions, prefer methods if you don't.
- Setting ConfigInvalidElement.Value throws an exception. Use ConfigInvalidElement.InvalidValue to set the default value which gets returned (Which is empty string by default)