-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathFileService.cs
45 lines (38 loc) · 1.31 KB
/
FileService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO;
using System.Text;
using System.Text.Json;
using DevHome.Common.Contracts;
namespace DevHome.Common.Services;
public class FileService : IFileService
{
#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.UTF8);
}
public void Delete(string folderPath, string fileName)
{
if (fileName != null && File.Exists(Path.Combine(folderPath, fileName)))
{
File.Delete(Path.Combine(folderPath, fileName));
}
}
}