mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
|
|
// Copyright (c) Microsoft Corporation
|
||
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||
|
|
// See the LICENSE file in the project root for more information.
|
||
|
|
|
||
|
|
using System.IO;
|
||
|
|
using System.Text;
|
||
|
|
using System.Text.Json;
|
||
|
|
using Microsoft.CmdPal.Common.Contracts;
|
||
|
|
|
||
|
|
namespace Microsoft.CmdPal.Common.Services;
|
||
|
|
|
||
|
|
public class FileService : IFileService
|
||
|
|
{
|
||
|
|
private static readonly Encoding _encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
|
||
|
|
|
||
|
|
#pragma warning disable CS8603 // Possible null reference return.
|
||
|
|
public T Read<T>(string folderPath, string fileName)
|
||
|
|
{
|
||
|
|
var path = Path.Combine(folderPath, fileName);
|
||
|
|
if (File.Exists(path))
|
||
|
|
{
|
||
|
|
using var fileStream = File.OpenText(path);
|
||
|
|
return JsonSerializer.Deserialize<T>(fileStream.BaseStream);
|
||
|
|
}
|
||
|
|
|
||
|
|
return default;
|
||
|
|
}
|
||
|
|
#pragma warning restore CS8603 // Possible null reference return.
|
||
|
|
|
||
|
|
public void Save<T>(string folderPath, string fileName, T content)
|
||
|
|
{
|
||
|
|
if (!Directory.Exists(folderPath))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(folderPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
var fileContent = JsonSerializer.Serialize(content);
|
||
|
|
File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, _encoding);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Delete(string folderPath, string fileName)
|
||
|
|
{
|
||
|
|
if (fileName != null && File.Exists(Path.Combine(folderPath, fileName)))
|
||
|
|
{
|
||
|
|
File.Delete(Path.Combine(folderPath, fileName));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|