pkar.NetConfigs 1.0.3

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

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

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.

See also my pkar.uwp.configs Nuget, as a wrapper with many extensions for UI elements

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 in 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)

or from WPF:

    Dim sAssemblyFullName = System.Reflection.Assembly.GetEntryAssembly().FullName
    Dim oAss As New AssemblyName(sAssemblyFullName)
    Dim sAppName = oAss.Name

    Dim sPathLocal As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), sAppName)
    Dim sPathRoam As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), sAppName)

    Vblib.InitSettings(
            sAppName, Environment.GetEnvironmentVariables(),
            Nothing,
            sPathLocal, sPathRoam,
            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

  • on reading, local values overrrides roaming values

      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)

You can also use my UwpConfigurationProvider from: https://github.com/pkar70/MyLibs/blob/ee7b8f3a7e7e28601426d4e4d20fd25f21f87628/UWPappTel/pkarModuleWithLib.vb#L2134 (something like 'config converter' from UWP settings to .Net settings)

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 (4)

Showing the top 4 NuGet packages that depend on pkar.NetConfigs:

Package Downloads
pkar.WPF.Configs

This Nuget adds UI extensions helpers to transfer data between UI elements and .Net configuration. It is similar to my other Nuget: pkar.Uwp.Config

pkar.winui3.Configs

This Nuget adds UI extensions helpers to transfer data between UI elements and .Net configuration. It is similar to my other Nuget: pkar.Uwp.Config and pkar.Wpf.Config

pkar.UWP.Config

UI extension for Nuget pkar.NetConfigs, with UWP ApplicationData to .Net configuration translator

pkar.MAUI.Configs

This Nuget adds UI extensions helpers to transfer data between UI elements and .Net configuration. It is similar to my other Nuget: pkar.Uwp.Config

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.5 270 5/6/2023
2.0.4 131 5/6/2023
2.0.3 167 4/20/2023
1.0.3 169 4/20/2023

Changed ID of package