4
4
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . IO ;
7
8
using System . Linq ;
8
9
using System . Threading . Tasks ;
9
10
using Microsoft . Toolkit . Helpers ;
@@ -282,27 +283,13 @@ public Task<bool> TryRenameItemAsync(string itemPath, string newName)
282
283
283
284
private async Task < T ? > ReadFileAsync < T > ( StorageFolder folder , string filePath , T ? @default = default )
284
285
{
285
- var dirName = System . IO . Path . GetDirectoryName ( filePath ) ;
286
- if ( ! string . IsNullOrEmpty ( dirName ) )
287
- {
288
- folder = await folder . GetFolderAsync ( dirName ) ;
289
- filePath = System . IO . Path . GetFileName ( filePath ) ;
290
- }
291
-
292
- string value = await StorageFileHelper . ReadTextFromFileAsync ( folder , filePath ) ;
286
+ string value = await StorageFileHelper . ReadTextFromFileAsync ( folder , NormalizePath ( filePath ) ) ;
293
287
return ( value != null ) ? this . Serializer . Deserialize < T > ( value ) : @default ;
294
288
}
295
289
296
290
private async Task < IEnumerable < ( DirectoryItemType , string ) > > ReadFolderAsync ( StorageFolder folder , string folderPath )
297
291
{
298
- var dirName = System . IO . Path . GetDirectoryName ( folderPath ) ;
299
- if ( ! string . IsNullOrEmpty ( dirName ) )
300
- {
301
- folder = await folder . GetFolderAsync ( dirName ) ;
302
- folderPath = System . IO . Path . GetFileName ( folderPath ) ;
303
- }
304
-
305
- var targetFolder = await folder . GetFolderAsync ( folderPath ) ;
292
+ var targetFolder = await folder . GetFolderAsync ( NormalizePath ( folderPath ) ) ;
306
293
var items = await targetFolder . GetItemsAsync ( ) ;
307
294
308
295
return items . Select ( ( item ) =>
@@ -317,33 +304,19 @@ public Task<bool> TryRenameItemAsync(string itemPath, string newName)
317
304
318
305
private async Task < StorageFile > CreateFileAsync < T > ( StorageFolder folder , string filePath , T value )
319
306
{
320
- var dirName = System . IO . Path . GetDirectoryName ( filePath ) ;
321
- if ( ! string . IsNullOrEmpty ( dirName ) )
322
- {
323
- folder = await folder . GetFolderAsync ( dirName ) ;
324
- filePath = System . IO . Path . GetFileName ( filePath ) ;
325
- }
326
-
327
- return await StorageFileHelper . WriteTextToFileAsync ( folder , this . Serializer . Serialize ( value ) ? . ToString ( ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
307
+ return await StorageFileHelper . WriteTextToFileAsync ( folder , this . Serializer . Serialize ( value ) ? . ToString ( ) , NormalizePath ( filePath ) , CreationCollisionOption . ReplaceExisting ) ;
328
308
}
329
309
330
310
private async Task CreateFolderAsync ( StorageFolder folder , string folderPath )
331
311
{
332
- var dirName = System . IO . Path . GetDirectoryName ( folderPath ) ;
333
- if ( ! string . IsNullOrEmpty ( dirName ) )
334
- {
335
- folder = await folder . GetFolderAsync ( dirName ) ;
336
- folderPath = System . IO . Path . GetFileName ( folderPath ) ;
337
- }
338
-
339
- await folder . CreateFolderAsync ( folderPath , CreationCollisionOption . OpenIfExists ) ;
312
+ await folder . CreateFolderAsync ( NormalizePath ( folderPath ) , CreationCollisionOption . OpenIfExists ) ;
340
313
}
341
314
342
315
private async Task < bool > TryDeleteItemAsync ( StorageFolder folder , string itemPath )
343
316
{
344
317
try
345
318
{
346
- var item = await folder . GetItemAsync ( itemPath ) ;
319
+ var item = await folder . GetItemAsync ( NormalizePath ( itemPath ) ) ;
347
320
await item . DeleteAsync ( ) ;
348
321
return true ;
349
322
}
@@ -357,7 +330,7 @@ private async Task<bool> TryRenameItemAsync(StorageFolder folder, string itemPat
357
330
{
358
331
try
359
332
{
360
- var item = await folder . GetItemAsync ( itemPath ) ;
333
+ var item = await folder . GetItemAsync ( NormalizePath ( itemPath ) ) ;
361
334
await item . RenameAsync ( newName , NameCollisionOption . FailIfExists ) ;
362
335
return true ;
363
336
}
@@ -366,5 +339,10 @@ private async Task<bool> TryRenameItemAsync(StorageFolder folder, string itemPat
366
339
return false ;
367
340
}
368
341
}
342
+
343
+ private string NormalizePath ( string path )
344
+ {
345
+ return Path . Combine ( Path . GetDirectoryName ( path ) , Path . GetFileName ( path ) ) ;
346
+ }
369
347
}
370
348
}
0 commit comments