2020-03-25 10:55:02 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Lib
|
|
|
|
|
{
|
|
|
|
|
public static class SettingsUtils
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
// Get path to the json settings file.
|
2020-03-25 10:55:02 +08:00
|
|
|
|
public static string GetSettingsPath(string powertoy)
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(powertoy))
|
2020-03-25 10:55:02 +08:00
|
|
|
|
{
|
|
|
|
|
return Path.Combine(
|
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
|
|
|
$"Microsoft\\PowerToys\\settings.json");
|
|
|
|
|
}
|
2020-04-08 01:19:14 +08:00
|
|
|
|
|
2020-03-25 10:55:02 +08:00
|
|
|
|
return Path.Combine(
|
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
|
|
|
$"Microsoft\\PowerToys\\{powertoy}\\settings.json");
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
// Get a Deserialized object of the json settings string.
|
2020-03-25 10:55:02 +08:00
|
|
|
|
public static T GetSettings<T>(string powertoy)
|
|
|
|
|
{
|
|
|
|
|
var jsonSettingsString = System.IO.File.ReadAllText(SettingsUtils.GetSettingsPath(powertoy));
|
|
|
|
|
return JsonSerializer.Deserialize<T>(jsonSettingsString);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
// Save settings to a json file.
|
|
|
|
|
public static void SaveSettings(string moduleJsonSettings, string powertoyModuleName)
|
2020-03-25 10:55:02 +08:00
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
System.IO.File.WriteAllText(
|
|
|
|
|
SettingsUtils.GetSettingsPath(powertoyModuleName),
|
|
|
|
|
moduleJsonSettings);
|
2020-03-25 10:55:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|