Skip to content

Commit 30f67b9

Browse files
authored
Fix: Fixed System.TypeInitializationException in FileTagsDatabase (#15647)
1 parent ccf4c36 commit 30f67b9

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/Files.App/Utils/FileTags/FileTagsDatabase.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ namespace Files.App.Utils.FileTags
1212
{
1313
public sealed class FileTagsDatabase
1414
{
15-
private readonly static string FileTagsKey = @$"Software\Files Community\{Package.Current.Id.FullName}\v1\FileTags";
15+
private static string? _FileTagsKey;
16+
private string? FileTagsKey => _FileTagsKey ??= SafetyExtensions.IgnoreExceptions(() => @$"Software\Files Community\{Package.Current.Id.FullName}\v1\FileTags");
1617

1718
public void SetTags(string filePath, ulong? frn, string[] tags)
1819
{
20+
if (FileTagsKey is null)
21+
return;
22+
1923
using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, filePath));
2024

2125
if (tags is [])
@@ -47,6 +51,9 @@ public void SetTags(string filePath, ulong? frn, string[] tags)
4751

4852
private TaggedFile? FindTag(string? filePath, ulong? frn)
4953
{
54+
if (FileTagsKey is null)
55+
return null;
56+
5057
if (filePath is not null)
5158
{
5259
using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, filePath));
@@ -87,6 +94,9 @@ public void SetTags(string filePath, ulong? frn, string[] tags)
8794

8895
public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
8996
{
97+
if (FileTagsKey is null)
98+
return;
99+
90100
var tag = FindTag(oldFilePath, null);
91101
using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, oldFilePath));
92102
SaveValues(filePathKey, null);
@@ -112,6 +122,9 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
112122

113123
public void UpdateTag(ulong oldFrn, ulong? frn, string? newFilePath)
114124
{
125+
if (FileTagsKey is null)
126+
return;
127+
115128
var tag = FindTag(null, oldFrn);
116129
using var frnKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, "FRN", oldFrn.ToString()));
117130
SaveValues(frnKey, null);
@@ -143,26 +156,31 @@ public string[] GetTags(string? filePath, ulong? frn)
143156
public IEnumerable<TaggedFile> GetAll()
144157
{
145158
var list = new List<TaggedFile>();
146-
IterateKeys(list, FileTagsKey, 0);
159+
160+
if (FileTagsKey is not null)
161+
IterateKeys(list, FileTagsKey, 0);
162+
147163
return list;
148164
}
149165

150166
public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
151167
{
152168
folderPath = folderPath.Replace('/', '\\').TrimStart('\\');
153169
var list = new List<TaggedFile>();
154-
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);
170+
171+
if (FileTagsKey is not null)
172+
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);
173+
155174
return list;
156175
}
157176

158177
public void Import(string json)
159178
{
179+
if (FileTagsKey is null)
180+
return;
181+
160182
var tags = JsonSerializer.Deserialize<TaggedFile[]>(json);
161-
ImportCore(tags);
162-
}
163183

164-
private static void ImportCore(TaggedFile[]? tags)
165-
{
166184
Registry.CurrentUser.DeleteSubKeyTree(FileTagsKey, false);
167185
if (tags is null)
168186
{
@@ -183,7 +201,10 @@ private static void ImportCore(TaggedFile[]? tags)
183201
public string Export()
184202
{
185203
var list = new List<TaggedFile>();
186-
IterateKeys(list, FileTagsKey, 0);
204+
205+
if (FileTagsKey is not null)
206+
IterateKeys(list, FileTagsKey, 0);
207+
187208
return JsonSerializer.Serialize(list);
188209
}
189210

0 commit comments

Comments
 (0)