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-04 10:02:38 +08:00
|
|
|
|
private static string LocalApplicationDataFolder()
|
|
|
|
|
{
|
|
|
|
|
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool SettingsFolderExists(string powertoy)
|
|
|
|
|
{
|
|
|
|
|
return Directory.Exists(Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CreateSettingsFolder(string powertoy)
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get path to the json settings file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>string path.</returns>
|
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(
|
2020-04-04 10:02:38 +08:00
|
|
|
|
LocalApplicationDataFolder(),
|
2020-03-25 10:55:02 +08:00
|
|
|
|
$"Microsoft\\PowerToys\\settings.json");
|
|
|
|
|
}
|
2020-04-08 01:19:14 +08:00
|
|
|
|
|
2020-03-25 10:55:02 +08:00
|
|
|
|
return Path.Combine(
|
2020-04-04 10:02:38 +08:00
|
|
|
|
LocalApplicationDataFolder(),
|
2020-03-25 10:55:02 +08:00
|
|
|
|
$"Microsoft\\PowerToys\\{powertoy}\\settings.json");
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 10:02:38 +08:00
|
|
|
|
public static bool SettingsExists(string powertoy)
|
|
|
|
|
{
|
|
|
|
|
return File.Exists(SettingsUtils.GetSettingsPath(powertoy));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get a Deserialized object of the json settings string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Deserialized json settings object.</returns>
|
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.
|
2020-04-04 10:02:38 +08:00
|
|
|
|
public static void SaveSettings(string jsonSettings, string powertoy)
|
2020-03-25 10:55:02 +08:00
|
|
|
|
{
|
2020-04-04 10:02:38 +08:00
|
|
|
|
if(jsonSettings != null)
|
|
|
|
|
{
|
|
|
|
|
if (!SettingsFolderExists(powertoy))
|
|
|
|
|
{
|
|
|
|
|
CreateSettingsFolder(powertoy);
|
|
|
|
|
}
|
|
|
|
|
System.IO.File.WriteAllText(
|
|
|
|
|
SettingsUtils.GetSettingsPath(powertoy),
|
|
|
|
|
jsonSettings);
|
|
|
|
|
}
|
2020-03-25 10:55:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|