mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Refactoring directory path for storage
This commit is contained in:
@@ -4,23 +4,18 @@ using System.IO;
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
[Serializable]
|
||||
public abstract class BaseStorage<T> : IStorage where T : class,IStorage, new()
|
||||
public abstract class BaseStorage<T> : IStorage where T : class, IStorage, new()
|
||||
{
|
||||
protected abstract string ConfigFolder { get; }
|
||||
protected string DirectoryPath { get; } = Path.Combine(WoxDirectroy.Executable, "Config");
|
||||
|
||||
protected string ConfigPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return Path.Combine(ConfigFolder, ConfigName + FileSuffix);
|
||||
}
|
||||
}
|
||||
protected string FilePath => Path.Combine(DirectoryPath, FileName + FileSuffix);
|
||||
|
||||
protected abstract string FileSuffix { get; }
|
||||
|
||||
protected abstract string ConfigName { get; }
|
||||
protected abstract string FileName { get; }
|
||||
|
||||
private static object locker = new object();
|
||||
|
||||
protected static T serializedObject;
|
||||
|
||||
public event Action<T> AfterLoad;
|
||||
@@ -64,13 +59,13 @@ namespace Wox.Infrastructure.Storage
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!File.Exists(ConfigPath))
|
||||
if (!File.Exists(FilePath))
|
||||
{
|
||||
if (!Directory.Exists(ConfigFolder))
|
||||
if (!Directory.Exists(DirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(ConfigFolder);
|
||||
Directory.CreateDirectory(DirectoryPath);
|
||||
}
|
||||
File.Create(ConfigPath).Close();
|
||||
File.Create(FilePath).Close();
|
||||
}
|
||||
LoadInternal();
|
||||
OnAfterLoad(serializedObject);
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Wox.Infrastructure.Storage
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||
try
|
||||
{
|
||||
using (FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
{
|
||||
if (fileStream.Length > 0)
|
||||
{
|
||||
@@ -93,7 +93,7 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create);
|
||||
FileStream fileStream = new FileStream(FilePath, FileMode.Create);
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter
|
||||
{
|
||||
AssemblyFormat = FormatterAssemblyStyle.Simple
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Wox.Infrastructure.Storage
|
||||
|
||||
protected override void LoadInternal()
|
||||
{
|
||||
string json = File.ReadAllText(ConfigPath);
|
||||
string json = File.ReadAllText(FilePath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
@@ -42,7 +42,7 @@ namespace Wox.Infrastructure.Storage
|
||||
lock (syncObject)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
File.WriteAllText(FilePath, json);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user