Nucs / JsonSettings

This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
MIT License
76 stars 18 forks source link

use custom path from static variable #1

Closed ghost1372 closed 6 years ago

ghost1372 commented 6 years ago

hi i have static variable and i want use in json settings this is my code but not work

public static string myPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MoalemYar\config.json";
private static SettingsBag Settings { get; } = JsonSettings.Construct<SettingsBag>(myPath).LoadNow().EnableAutosave();
Nucs commented 6 years ago

I can confirm it is a bug, the library should ensure the directory always exists and LoadNow() should be able to handle empty config files or when they don't even exist. Use it in the following manner:

class Program {
    static Program() {
        //Ensure the directory exists.
        Directory.CreateDirectory(Path.GetDirectoryName(myPath));
        Settings = JsonSettings.Load<SettingsBag>(myPath).EnableAutosave();
    }

    public static string myPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MoalemYar\config.json";
    private static SettingsBag Settings { get; }

    static void Main(string[] args) {
        Settings["testprop"] = 1;
        Console.ReadLine();
    }
}

Will fix it ASAP, thanks.

Nucs commented 6 years ago

Fixed it. Will upload to nuget as version 1.0.2 soon. Closed.