Giter Site home page Giter Site logo

mijenner.configutils-usersettings's Introduction

MiJenner.ConfigUtils-UserSettings

Offers functionality to store a settings object to/from json files. It includes an implementation using Microsofts Microsoft.Text.Json and another implementation Newtonsofts Newtonsoft.json.

Getting started

Add using statement

using MiJenner.ConfigUtils; 

Create Settings class and object

First create the class and object you want to hold user settings in the application, e.g.

public class Settings
{
   public string MyString { get; set; } = "DefaultName";
   public int MyInt { get; set; } = 25;
   public bool MyBool { get; set; } = false;
   public double MyDouble { get; set; } = 3.1425;
}

Note: It is important that you define default values for the various settings like in the example above.

Somewhere else in your code you instantiate an object of above type, e.g.:

Settings settings = new Settings() { MyString = "set1", MyInt = 5, MyBool = true, MyDouble = 14.16 };

Create UserSettings handler

Now create an instance of the UserSettings handler:

IUserSettingsHandler<Settings> settingsHandler = new JsonUserSettingsHandlerMS<Settings>("usersettings.json");

Note: above file specification will use current directory which you do not want. Use e.g. [link]https://github.com/mijenner/MiJenner.ConfigUtils-FolderManager to determine a proper full path like C:\Users\john\AppData\Roaming\YourCompany\AppName\usersettings.json.

Write settings to json file

When needed you can easily try to write values in settings object to the json file:

settingsHandler.TryWrite(settings)

Note: TryWrite returns a boolean depending on success or failure.

Read settings from json file

In the beginning of loading your application you may want to try to read the settings from a json file and into your application. Here you can use:

settingsHandler.TryRead(out settingsb)

Note: TryRead returns a boolean depending on success or failure.

NuGet package

For ease of use there is a NuGet package available.

More advanced solutions

If you are looking for a more advanced solution, with event based triggering etc ..., please consider https://github.com/anakic/Jot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.