sharpconfig 3.2.9.1

dotnet add package sharpconfig --version 3.2.9.1
NuGet\Install-Package sharpconfig -Version 3.2.9.1
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="sharpconfig" Version="3.2.9.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add sharpconfig --version 3.2.9.1
#r "nuget: sharpconfig, 3.2.9.1"
#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 sharpconfig as a Cake Addin
#addin nuget:?package=sharpconfig&version=3.2.9.1

// Install sharpconfig as a Cake Tool
#tool nuget:?package=sharpconfig&version=3.2.9.1

SharpConfig

NuGet version

SharpConfig is an easy to use CFG/INI configuration library for .NET.

You can use SharpConfig to read, modify and save configuration files and streams, in either text or binary format.

The library is fully compatible with .NET, .NET Core and the Mono Framework.

Visit repository on SourceHut

.NET CLI: > dotnet add package sharpconfig

Package Manager: PM> NuGet\Install-Package sharpconfig

An example Configuration

[General]
# a comment
SomeString = Hello World!
SomeInteger = 10 # an inline comment
SomeFloat = 20.05
SomeBoolean = true
SomeArray = { 1, 2, 3 }
Day = Monday

[Person]
Name = Peter
Age = 50

To read these values, your C# code would look like:

var config = Configuration.LoadFromFile("sample.cfg");
var section = config["General"];

string someString = section["SomeString"].StringValue;
int someInteger = section["SomeInteger"].IntValue;
float someFloat = section["SomeFloat"].FloatValue;
bool someBool = section["SomeBoolean"].BoolValue;
int[] someIntArray = section["SomeArray"].IntValueArray;
string[] someStringArray = section["SomeArray"].StringValueArray;
DayOfWeek day = section["Day"].GetValue<DayOfWeek>();

// Entire user-defined objects can be created from sections and vice versa.
var person = config["Person"].ToObject<Person>();
// ...

Iterating through a Configuration

foreach (var section in myConfig)
{
    foreach (var setting in section)
    {
        // ...
    }
}

Creating a Configuration in memory

// Create the configuration.
var myConfig = new Configuration();

// Set some values.
// This will automatically create the sections and settings.
myConfig["Video"]["Width"].IntValue = 1920;
myConfig["Video"]["Height"].IntValue = 1080;

// Set an array value.
myConfig["Video"]["Formats"].StringValueArray = new[] { "RGB32", "RGBA32" };

// Get the values just to test.
int width = myConfig["Video"]["Width"].IntValue;
int height = myConfig["Video"]["Height"].IntValue;
string[] formats = myConfig["Video"]["Formats"].StringValueArray;
// ...

Loading a Configuration

Configuration.LoadFromFile("myConfig.cfg");        // Load from a text-based file.
Configuration.LoadFromStream(myStream);            // Load from a text-based stream.
Configuration.LoadFromString(myString);            // Load from text (source code).
Configuration.LoadFromBinaryFile("myConfig.cfg");  // Load from a binary file.
Configuration.LoadFromBinaryStream(myStream);      // Load from a binary stream.

Saving a Configuration

myConfig.SaveToFile("myConfig.cfg");        // Save to a text-based file.
myConfig.SaveToStream(myStream);            // Save to a text-based stream.
myConfig.SaveToBinaryFile("myConfig.cfg");  // Save to a binary file.
myConfig.SaveToBinaryStream(myStream);      // Save to a binary stream.

Options

Sometimes a project has special configuration files or other needs, for example ignoring all comments in a file. To allow for greater flexibility, SharpConfig's behavior is modifiable using static properties of the Configuration class. The following properties are the current ones:

  • CultureInfo Configuration.CultureInfo { get; set; }

    • Gets or sets the CultureInfo that is used for value conversion in SharpConfig. The default value is CultureInfo.InvariantCulture.
  • char[] Configuration.ValidCommentChars { get; }

    • Gets the array that contains all valid comment delimiting characters. The current value is { '#', ';' }.
  • char Configuration.PreferredCommentChar { get; set; }

    • Gets or sets the preferred comment char when saving configurations. The default value is '#'.
  • char Configuration.ArrayElementSeparator { get; set; }

    • Gets or sets the array element separator character for settings. The default value is ','.
    • Remember that after you change this value while Setting instances exist, to expect their ArraySize and other array-related values to return different values.
  • bool Configuration.IgnoreInlineComments { get; set; }

    • Gets or sets a value indicating whether inline-comments should be ignored when parsing a configuration.
  • bool Configuration.IgnorePreComments { get; set; }

    • Gets or sets a value indicating whether pre-comments should be ignored when parsing a configuration.
  • bool Configuration.SpaceBetweenEquals { get; set; }

    • Gets or sets a value indicating whether space between equals should be added when creating a configuration.
  • bool Configuration.OutputRawStringValues { get; set; }

    • Gets or sets a value indicating whether string values are written without quotes, but including everything in between. Example:
      • The setting MySetting=" Example value" is written to a file in as MySetting= Example value

Ignoring properties, fields and types

Suppose you have the following class:

class SomeClass
{
    public string Name { get; set; }
    public int Age { get; set; }

    [SharpConfig.Ignore]
    public int SomeInt { get; set; }
}

SharpConfig will now ignore the SomeInt property when creating sections from objects of type SomeClass and vice versa. Now suppose you have a type in your project that should always be ignored. You would have to mark every property that returns this type with a [SharpConfig.Ignore] attribute. An easier solution is to just apply the [SharpConfig.Ignore] attribute to the type.

Example:

[SharpConfig.Ignore]
class ThisTypeShouldAlwaysBeIgnored
{
    // ...
}

instead of:

[SharpConfig.Ignore]
class SomeClass
{
    [SharpConfig.Ignore]
    public ThisTypeShouldAlwaysBeIgnored T1 { get; set; }

    [SharpConfig.Ignore]
    public ThisTypeShouldAlwaysBeIgnored T2 { get; set; }

    [SharpConfig.Ignore]
    public ThisTypeShouldAlwaysBeIgnored T3 { get; set; }
}

This ignoring mechanism works the same way for public fields.

Adding custom object converters

There may be cases where you want to implement conversion rules for a custom type, with specific requirements. This is easy and involves two steps, which are illustrated using the Person example:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Step 1: Create a custom converter class that derives from SharpConfig.TypeStringConverter<T>:

using SharpConfig;
public class PersonStringConverter : TypeStringConverter<Person>
{
    // This method is responsible for converting a Person object to a string.
    public override string ConvertToString(object value)
    {
        var person = (Person)value;
        return string.Format("[{0};{1}]", person.Name, person.Age);
    }

    // This method is responsible for converting a string to a Person object.
    public override object ConvertFromString(string value, Type hint)
    {
        var split = value.Trim('[', ']').Split(';');

        var person = new Person();
        person.Name = split[0];
        person.Age = int.Parse(split[1]);

        return person;
     }
}

Step 2: Register the PersonStringConverter (anywhere you like):

using SharpConfig;
Configuration.RegisterTypeStringConverter(new PersonStringConverter());

That's it!

Whenever a Person object is used on a Setting (via GetValue() and SetValue()), your converter is selected to take care of the conversion. This also automatically works with SetValue() for arrays and GetValueArray().

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 net20 is compatible.  net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 is compatible.  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.
  • .NETFramework 2.0

    • No dependencies.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on sharpconfig:

Package Downloads
Chuma.AlphaMS.Aliyun

初码-AlphaMS开发库-阿里云库

Etor.Infrastructure

Package Description

Karpach.Remote.Commands.Base

Base class for Remote Controller commands (Karpach.Remote.Commander)

EasyLOB.Extensions.Ini

EasyLOB Extensions INI

Kitty.Common_framework

this library contains log config inject database

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on sharpconfig:

Repository Stars
micahmo/WgServerforWindows
Wg Server for Windows (WS4W) is a desktop application that allows running and managing a WireGuard server endpoint on Windows
ciribob/DCS-SimpleRadioStandalone
An open source Stand alone Radio for DCS integrating with all clickable cockpits and FC3 Aircraft
Version Downloads Last updated
3.2.9.1 73,995 5/20/2020
3.2.9 1,504 5/19/2020
3.2.8 100,757 4/9/2018
3.2.6.1 2,593 3/8/2018
3.2.6 2,172 3/4/2018
3.2.5 2,318 12/12/2017
3.2.4 1,680 12/11/2017
3.2.3 1,661 12/7/2017
3.2.1 1,636 12/6/2017
3.2.0 3,029 7/7/2017
3.1.0 3,720 3/31/2017
3.0.1 2,683 12/5/2016
3.0.0 1,644 12/4/2016
2.1.0 5,457 11/7/2016
2.0.1 2,162 9/5/2016
2.0.0 2,948 8/21/2016
1.5.6 5,761 7/12/2016
1.5.5 4,335 6/1/2016
1.5.4 5,116 4/21/2016
1.5.3 1,704 4/11/2016
1.5.2 2,237 3/22/2016
1.5.1 1,677 3/4/2016
1.5.0 1,635 3/3/2016
1.4.9 7,271 12/16/2015
1.4.8 2,187 12/4/2015
1.4.7 1,946 11/30/2015
1.4.6 1,928 11/29/2015
1.4.4 2,238 8/28/2015
1.4.3 4,172 3/31/2015
1.4.2 1,924 1/31/2015
1.4.1 1,723 1/30/2015
1.4.0 2,473 9/26/2014
1.3.0 2,660 10/30/2013
1.2.0 1,902 10/29/2013

Please see the project URL for the latest release notes.