@@ -12,10 +12,14 @@ namespace Files.App.Utils.FileTags
12
12
{
13
13
public sealed class FileTagsDatabase
14
14
{
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") ;
16
17
17
18
public void SetTags ( string filePath , ulong ? frn , string [ ] tags )
18
19
{
20
+ if ( FileTagsKey is null )
21
+ return ;
22
+
19
23
using var filePathKey = Registry . CurrentUser . CreateSubKey ( CombineKeys ( FileTagsKey , filePath ) ) ;
20
24
21
25
if ( tags is [ ] )
@@ -47,6 +51,9 @@ public void SetTags(string filePath, ulong? frn, string[] tags)
47
51
48
52
private TaggedFile ? FindTag ( string ? filePath , ulong ? frn )
49
53
{
54
+ if ( FileTagsKey is null )
55
+ return null ;
56
+
50
57
if ( filePath is not null )
51
58
{
52
59
using var filePathKey = Registry . CurrentUser . CreateSubKey ( CombineKeys ( FileTagsKey , filePath ) ) ;
@@ -87,6 +94,9 @@ public void SetTags(string filePath, ulong? frn, string[] tags)
87
94
88
95
public void UpdateTag ( string oldFilePath , ulong ? frn , string ? newFilePath )
89
96
{
97
+ if ( FileTagsKey is null )
98
+ return ;
99
+
90
100
var tag = FindTag ( oldFilePath , null ) ;
91
101
using var filePathKey = Registry . CurrentUser . CreateSubKey ( CombineKeys ( FileTagsKey , oldFilePath ) ) ;
92
102
SaveValues ( filePathKey , null ) ;
@@ -112,6 +122,9 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
112
122
113
123
public void UpdateTag ( ulong oldFrn , ulong ? frn , string ? newFilePath )
114
124
{
125
+ if ( FileTagsKey is null )
126
+ return ;
127
+
115
128
var tag = FindTag ( null , oldFrn ) ;
116
129
using var frnKey = Registry . CurrentUser . CreateSubKey ( CombineKeys ( FileTagsKey , "FRN" , oldFrn . ToString ( ) ) ) ;
117
130
SaveValues ( frnKey , null ) ;
@@ -143,26 +156,31 @@ public string[] GetTags(string? filePath, ulong? frn)
143
156
public IEnumerable < TaggedFile > GetAll ( )
144
157
{
145
158
var list = new List < TaggedFile > ( ) ;
146
- IterateKeys ( list , FileTagsKey , 0 ) ;
159
+
160
+ if ( FileTagsKey is not null )
161
+ IterateKeys ( list , FileTagsKey , 0 ) ;
162
+
147
163
return list ;
148
164
}
149
165
150
166
public IEnumerable < TaggedFile > GetAllUnderPath ( string folderPath )
151
167
{
152
168
folderPath = folderPath . Replace ( '/' , '\\ ' ) . TrimStart ( '\\ ' ) ;
153
169
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
+
155
174
return list ;
156
175
}
157
176
158
177
public void Import ( string json )
159
178
{
179
+ if ( FileTagsKey is null )
180
+ return ;
181
+
160
182
var tags = JsonSerializer . Deserialize < TaggedFile [ ] > ( json ) ;
161
- ImportCore ( tags ) ;
162
- }
163
183
164
- private static void ImportCore ( TaggedFile [ ] ? tags )
165
- {
166
184
Registry . CurrentUser . DeleteSubKeyTree ( FileTagsKey , false ) ;
167
185
if ( tags is null )
168
186
{
@@ -183,7 +201,10 @@ private static void ImportCore(TaggedFile[]? tags)
183
201
public string Export ( )
184
202
{
185
203
var list = new List < TaggedFile > ( ) ;
186
- IterateKeys ( list , FileTagsKey , 0 ) ;
204
+
205
+ if ( FileTagsKey is not null )
206
+ IterateKeys ( list , FileTagsKey , 0 ) ;
207
+
187
208
return JsonSerializer . Serialize ( list ) ;
188
209
}
189
210
0 commit comments