NetConfigs 1.0.1

Suggested Alternatives

pkar.NetConfigs

Additional Details

Since 1.0.3, I added prefix 'pkar' to package ID.

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

// Install NetConfigs as a Cake Tool
#tool nuget:?package=NetConfigs&version=1.0.1                

This package contains four of my Microsoft.Extensions.Configuration.ConfigurationProvider / ConfigurationSource pairs, and some convenient methods to Get/Set values.

I was "forced" to make these version when I started to porting my UWP apps to multiplatform, and I wanted analogs of Windows.Storage.ApplicationData.Current..RoamingSettings and .LocalSettings. Also, I want it to work on phones (so it is limited to .Net Standard 1.4, last that works on Windows 10.15063)

So, I have these requirements:

  • work in Plarform Uno, phones, and Win7 ⇒ .Net Standard 1.4
  • have both roam and local settings
  • can use only runtime files, not files in installation package (Android doesn't extract files from package)

Basic idea: when .Set(variableName, value), and value is prefixed with "[roam]", it sets roaming setting; else sets local setting.

settings methods

initialization

Without init, you get only read/write config in temporaty JSON file (in user temp directory).

You can init library by using fully customizable IConfigurationRoot

Sub InitSettings(settings As MsExtConf.IConfigurationRoot)

where settings is result of something like this:

    Dim sAppName As String = Windows.ApplicationModel.Package.Current.DisplayName
    Dim oBuilder As New Microsoft.Extensions.Configuration.ConfigurationBuilder()
    Dim oDict As IDictionary = Environment.GetEnvironmentVariables()
    oBuilder = oBuilder.AddEnvironmentVariablesROConfigurationSource(sAppName, oDict) 
    oBuilder = oBuilder.AddUwpSettings()
    oBuilder = oBuilder.AddJsonRwSettings(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
                    Windows.Storage.ApplicationData.Current.RoamingFolder.Path)
    If aCmdLineArgs IsNot Nothing Then oBuilder = oBuilder.AddCommandLineRO(aCmdLineArgs)
    Dim settings As Microsoft.Extensions.Configuration.IConfigurationRoot = oBuilder.Build
    pkar.NetConfigs.InitSettings(settings As MsExtConf.IConfigurationRoot)

or use simplified form:

    Sub InitSettings(sINIcontent As String, bIniUseDebug As Boolean,
                        applicationName As String, dictionaryOfEnvVars As System.Collections.IDictionary,
                        configSource As MsExtConf.IConfigurationSource,
                        localJSONdirName As String, roamJSONdirNname As String, bJSONreadOnly As Boolean,
                        cmdLineArgs As List(Of String))

as this call (from UWP):

    #if DEBUG
    pkar.NetConfigs.InitSettings(sINIcontent, True,
    #else
    pkar.NetConfigs.InitSettings(sINIcontent, False,
    #end if
                Windows.ApplicationModel.Package.Current.DisplayName, Environment.GetEnvironmentVariables(),
                new UwpConfigurationSource,
                Windows.Storage.ApplicationData.Current.LocalFolder.Path, Windows.Storage.ApplicationData.Current.RoamingFolder.Path, False,
                Environment.GetCommandLineArgs.ToList)

You can use NULLs if you don't want particular ConfigurationSource

setting values

Sub SetSettingsString(sName As String, value As String, Optional bRoam As Boolean = False)
Sub SetSettingsInt(sName As String, value As Integer, Optional bRoam As Boolean = False)
Sub SetSettingsBool(sName As String, value As Boolean, Optional bRoam As Boolean = False)
Sub SetSettingsString(sName, value.ToString(System.Globalization.CultureInfo.InvariantCulture), bRoam)
Sub SetSettingsDate(sName As String, value As DateTimeOffset, Optional bRoam As Boolean = False)
Sub SetSettingsCurrentDate(sName As String, Optional bRoam As Boolean = False)

getting values

Function GetSettingsString(sName As String, Optional sDefault As String = "") As String
Function GetSettingsInt(sName As String, Optional iDefault As Integer = 0) As Integer
Function GetSettingsBool(sName As String, Optional bDefault As Boolean = False) As Boolean
Function GetSettingsLong(sName As String, Optional iDefault As Long = 0) As Long
Function GetSettingsDate(sName As String, Optional sDefault As String = "") As DateTimeOffset
Function GetSettingsDate(sName As String, dDefault As DateTimeOffset) As DateTimeOffset

providers

JsonRwConfiguration

Difference with Microsoft's implementation:

  • Microsoft's implementation doesn't work in .Net Standard 1.4, so it cannot be used on phones

  • Ms version is read-only

  • limit of this implementation: no tree of values, only flat version

  • we can have two files for values - local and roaming

      IConfigurationBuilder.AddJsonRwSettings(sPathnameLocal As String, sPathnameRoam As String, Optional bReadOnly As Boolean = False)
    

At least one pathname should be not null.

e.g.

    oBuilder.AddJsonRwSettings(Windows.Storage.ApplicationData.Current.LocalFolder.Path, Windows.Storage.ApplicationData.Current.RoamingFolder.Path);

IniDefaultsConfigurationProvider

Difference with Microsoft's implementation:

  • ctor uses not file name, but file content - e.g. on Android we have no files extracted from installation package

  • .Set is converted to .Remove, important escpecially when used in pack with my others ConfigurationProviders which react for [roam] prefix

      IConfigurationBuilder.AddIniReleaseDebugSettings(sIniContent As String, bUseDebug As Boolean)
    

e.g.

#if DEBUG
oBuilder = oBuilder.AddIniRelDebugSettings("...", true)
#else
oBuilder = oBuilder.AddIniRelDebugSettings("...", false)
#endif

EnvironmentVariablesROConfigurationProvider

Difference with Microsoft's implementation:

  • Microsoft's implementation doesn't work in .Net Standard 1.4, so it cannot be used on phones

  • .Set is converted to .Remove, important escpecially when used in pack with my others ConfigurationProviders which react for [roam] prefix

      IConfigurationBuilder.AddEnvironmentVariablesROConfigurationSource(sPrefix As String, oDict As System.Collections.IDictionary)
    

e.g.

oBuilder.AddEnvironmentVariablesROConfigurationSource(Windows.ApplicationModel.Package.Current.DisplayName, Environment.GetEnvironmentVariables())

(Environment.GetEnvironmentVariables() is visible in UWP app, but not in .Net Standard 1.4)

CommandLineROConfigurationProvider

Difference with Microsoft's implementation:

  • no tree of values

  • .Set is converted to .Remove, important escpecially when used in pack with my others ConfigurationProviders which react for [roam] prefix

      IConfigurationBuilder.AddCommandLineRO(aArgs As List(Of String))
    

e.g.

oBuilder = oBuilder.AddCommandLineRO(Environment.GetCommandLineArgs.ToList)

(Environment.GetCommandLineArgs() is visible in UWP app, but not in .Net Standard 1.4)

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 netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.4 is compatible.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  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 tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Added XML documentation