SettingPath.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.IO.Abstractions; 7 8 using Microsoft.PowerToys.Settings.UI.Library.Utilities; 9 10 namespace Microsoft.PowerToys.Settings.UI.Library 11 { 12 public class SettingPath 13 { 14 private const string DefaultFileName = "settings.json"; 15 16 private readonly IDirectory _directory; 17 18 private readonly IPath _path; 19 20 public SettingPath(IDirectory directory, IPath path) 21 { 22 _directory = directory ?? throw new ArgumentNullException(nameof(directory)); 23 _path = path ?? throw new ArgumentNullException(nameof(path)); 24 } 25 26 public SettingPath() 27 : this(new FileSystem().Directory, new FileSystem().Path) 28 { 29 } 30 31 public bool SettingsFolderExists(string powertoy) 32 { 33 return _directory.Exists(System.IO.Path.Combine(Helper.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}")); 34 } 35 36 public void CreateSettingsFolder(string powertoy) 37 { 38 _directory.CreateDirectory(System.IO.Path.Combine(Helper.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}")); 39 } 40 41 public void DeleteSettings(string powertoy = "") 42 { 43 _directory.Delete(System.IO.Path.Combine(Helper.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}")); 44 } 45 46 /// <summary> 47 /// Get path to the json settings file. 48 /// </summary> 49 /// <returns>string path.</returns> 50 public string GetSettingsPath(string powertoy, string fileName = DefaultFileName) 51 { 52 if (string.IsNullOrWhiteSpace(powertoy)) 53 { 54 return _path.Combine( 55 Helper.LocalApplicationDataFolder(), 56 $"Microsoft\\PowerToys\\{fileName}"); 57 } 58 59 return _path.Combine( 60 Helper.LocalApplicationDataFolder(), 61 $"Microsoft\\PowerToys\\{powertoy}\\{fileName}"); 62 } 63 } 64 }