@@ -197,6 +197,17 @@ await DialogDisplayHelper.ShowDialogAsync(
197
197
}
198
198
fsResult = fsCopyResult ;
199
199
}
200
+ if ( fsResult == FileSystemStatusCode . Unauthorized )
201
+ {
202
+ fsResult = await PerformAdminOperation ( new ValueSet ( )
203
+ {
204
+ { "Arguments" , "FileOperation" } ,
205
+ { "fileop" , "CopyItem" } ,
206
+ { "filepath" , source . Path } ,
207
+ { "destpath" , destination } ,
208
+ { "overwrite" , collision == NameCollisionOption . ReplaceExisting }
209
+ } ) ;
210
+ }
200
211
errorCode ? . Report ( fsResult . ErrorCode ) ;
201
212
if ( ! fsResult )
202
213
{
@@ -234,6 +245,17 @@ await DialogDisplayHelper.ShowDialogAsync(
234
245
}
235
246
fsResult = fsResultCopy ;
236
247
}
248
+ if ( fsResult == FileSystemStatusCode . Unauthorized )
249
+ {
250
+ fsResult = await PerformAdminOperation ( new ValueSet ( )
251
+ {
252
+ { "Arguments" , "FileOperation" } ,
253
+ { "fileop" , "CopyItem" } ,
254
+ { "filepath" , source . Path } ,
255
+ { "destpath" , destination } ,
256
+ { "overwrite" , collision == NameCollisionOption . ReplaceExisting }
257
+ } ) ;
258
+ }
237
259
}
238
260
errorCode ? . Report ( fsResult . ErrorCode ) ;
239
261
if ( ! fsResult )
@@ -387,6 +409,17 @@ await DialogDisplayHelper.ShowDialogAsync(
387
409
}
388
410
fsResult = fsResultMove ;
389
411
}
412
+ if ( fsResult == FileSystemStatusCode . Unauthorized || fsResult == FileSystemStatusCode . ReadOnly )
413
+ {
414
+ fsResult = await PerformAdminOperation ( new ValueSet ( )
415
+ {
416
+ { "Arguments" , "FileOperation" } ,
417
+ { "fileop" , "MoveItem" } ,
418
+ { "filepath" , source . Path } ,
419
+ { "destpath" , destination } ,
420
+ { "overwrite" , collision == NameCollisionOption . ReplaceExisting }
421
+ } ) ;
422
+ }
390
423
}
391
424
errorCode ? . Report ( fsResult . ErrorCode ) ;
392
425
}
@@ -406,7 +439,7 @@ await DialogDisplayHelper.ShowDialogAsync(
406
439
if ( fsResult )
407
440
{
408
441
var file = ( StorageFile ) sourceResult ;
409
- var fsResultMove = await FilesystemTasks . Wrap ( ( ) => file . MoveAsync ( destinationResult . Result , Path . GetFileName ( file . Name ) , NameCollisionOption . FailIfExists ) . AsTask ( ) ) ;
442
+ var fsResultMove = await FilesystemTasks . Wrap ( ( ) => file . MoveAsync ( destinationResult . Result , Path . GetFileName ( file . Name ) , collision ) . AsTask ( ) ) ;
410
443
411
444
if ( fsResultMove == FileSystemStatusCode . AlreadyExists )
412
445
{
@@ -421,6 +454,17 @@ await DialogDisplayHelper.ShowDialogAsync(
421
454
}
422
455
fsResult = fsResultMove ;
423
456
}
457
+ if ( fsResult == FileSystemStatusCode . Unauthorized || fsResult == FileSystemStatusCode . ReadOnly )
458
+ {
459
+ fsResult = await PerformAdminOperation ( new ValueSet ( )
460
+ {
461
+ { "Arguments" , "FileOperation" } ,
462
+ { "fileop" , "MoveItem" } ,
463
+ { "filepath" , source . Path } ,
464
+ { "destpath" , destination } ,
465
+ { "overwrite" , collision == NameCollisionOption . ReplaceExisting }
466
+ } ) ;
467
+ }
424
468
}
425
469
errorCode ? . Report ( fsResult . ErrorCode ) ;
426
470
}
@@ -516,28 +560,13 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source,
516
560
}
517
561
if ( ! fsResult )
518
562
{
519
- var elevateConfirmDialog = new Files . Dialogs . ElevateConfirmDialog ( ) ;
520
- var elevateConfirmResult = await elevateConfirmDialog . ShowAsync ( ) ;
521
- if ( elevateConfirmResult == ContentDialogResult . Primary )
563
+ fsResult = await PerformAdminOperation ( new ValueSet ( )
522
564
{
523
- if ( associatedInstance . ServiceConnection != null
524
- && await associatedInstance . ServiceConnection . Elevate ( ) )
525
- {
526
- // Try again with fulltrust process (admin)
527
- if ( associatedInstance . ServiceConnection != null )
528
- {
529
- var ( status , response ) = await associatedInstance . ServiceConnection . SendMessageForResponseAsync ( new ValueSet ( )
530
- {
531
- { "Arguments" , "FileOperation" } ,
532
- { "fileop" , "DeleteItem" } ,
533
- { "filepath" , source . Path } ,
534
- { "permanently" , permanently }
535
- } ) ;
536
- fsResult = ( FilesystemResult ) ( status == AppServiceResponseStatus . Success
537
- && response . Get ( "Success" , false ) ) ;
538
- }
539
- }
540
- }
565
+ { "Arguments" , "FileOperation" } ,
566
+ { "fileop" , "DeleteItem" } ,
567
+ { "filepath" , source . Path } ,
568
+ { "permanently" , permanently }
569
+ } ) ;
541
570
}
542
571
}
543
572
else if ( fsResult == FileSystemStatusCode . InUse )
@@ -649,7 +678,19 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
649
678
}
650
679
else
651
680
{
652
- Debug . WriteLine ( System . Runtime . InteropServices . Marshal . GetLastWin32Error ( ) ) ;
681
+ var fsResult = await PerformAdminOperation ( new ValueSet ( )
682
+ {
683
+ { "Arguments" , "FileOperation" } ,
684
+ { "fileop" , "RenameItem" } ,
685
+ { "filepath" , source . Path } ,
686
+ { "newName" , newName } ,
687
+ { "overwrite" , collision == NameCollisionOption . ReplaceExisting }
688
+ } ) ;
689
+ if ( fsResult )
690
+ {
691
+ errorCode ? . Report ( FileSystemStatusCode . Success ) ;
692
+ return new StorageHistory ( FileOperationType . Rename , source , StorageItemHelpers . FromPathAndType ( destination , source . ItemType ) ) ;
693
+ }
653
694
}
654
695
}
655
696
else if ( renamed == FileSystemStatusCode . NotAFile || renamed == FileSystemStatusCode . NotAFolder )
@@ -711,50 +752,51 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
711
752
FilesystemResult fsResult = FileSystemStatusCode . InProgress ;
712
753
errorCode ? . Report ( fsResult ) ;
713
754
714
- if ( source . ItemType == FilesystemItemType . Directory )
755
+ fsResult = ( FilesystemResult ) await Task . Run ( ( ) => NativeFileOperationsHelper . MoveFileFromApp ( source . Path , destination ) ) ;
756
+
757
+ if ( ! fsResult )
715
758
{
716
- FilesystemResult < StorageFolder > sourceFolder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( source . Path ) ;
717
- FilesystemResult < StorageFolder > destinationFolder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( destination ) ) ;
759
+ if ( source . ItemType == FilesystemItemType . Directory )
760
+ {
761
+ FilesystemResult < StorageFolder > sourceFolder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( source . Path ) ;
762
+ FilesystemResult < StorageFolder > destinationFolder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( destination ) ) ;
718
763
719
- fsResult = sourceFolder . ErrorCode | destinationFolder . ErrorCode ;
720
- errorCode ? . Report ( fsResult ) ;
764
+ fsResult = sourceFolder . ErrorCode | destinationFolder . ErrorCode ;
765
+ errorCode ? . Report ( fsResult ) ;
721
766
722
- if ( fsResult )
723
- {
724
- fsResult = await FilesystemTasks . Wrap ( ( ) =>
767
+ if ( fsResult )
725
768
{
726
- return MoveDirectoryAsync ( sourceFolder . Result ,
727
- destinationFolder . Result ,
728
- Path . GetFileName ( destination ) ,
729
- CreationCollisionOption . FailIfExists ,
730
- true ) ;
731
- } ) ; // TODO: we could use here FilesystemHelpers with registerHistory false?
769
+ fsResult = await FilesystemTasks . Wrap ( ( ) => MoveDirectoryAsync ( sourceFolder . Result , destinationFolder . Result , Path . GetFileName ( destination ) ,
770
+ CreationCollisionOption . FailIfExists , true ) ) ;
771
+ // TODO: we could use here FilesystemHelpers with registerHistory false?
772
+ }
773
+ errorCode ? . Report ( fsResult ) ;
732
774
}
733
- errorCode ? . Report ( fsResult ) ;
734
- }
735
- else
736
- {
737
- FilesystemResult < StorageFile > sourceFile = await associatedInstance . FilesystemViewModel . GetFileFromPathAsync ( source . Path ) ;
738
- FilesystemResult < StorageFolder > destinationFolder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( destination ) ) ;
775
+ else
776
+ {
777
+ FilesystemResult < StorageFile > sourceFile = await associatedInstance . FilesystemViewModel . GetFileFromPathAsync ( source . Path ) ;
778
+ FilesystemResult < StorageFolder > destinationFolder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( destination ) ) ;
739
779
740
- fsResult = sourceFile . ErrorCode | destinationFolder . ErrorCode ;
741
- errorCode ? . Report ( fsResult ) ;
780
+ fsResult = sourceFile . ErrorCode | destinationFolder . ErrorCode ;
781
+ errorCode ? . Report ( fsResult ) ;
742
782
743
- if ( fsResult )
744
- {
745
- fsResult = await FilesystemTasks . Wrap ( ( ) =>
783
+ if ( fsResult )
746
784
{
747
- return sourceFile . Result . MoveAsync ( destinationFolder . Result ,
748
- Path . GetFileName ( destination ) ,
749
- NameCollisionOption . GenerateUniqueName ) . AsTask ( ) ;
750
- } ) ;
785
+ fsResult = await FilesystemTasks . Wrap ( ( ) => sourceFile . Result . MoveAsync ( destinationFolder . Result , Path . GetFileName ( destination ) , NameCollisionOption . GenerateUniqueName ) . AsTask ( ) ) ;
786
+ }
787
+ errorCode ? . Report ( fsResult ) ;
751
788
}
752
- else if ( fsResult == FileSystemStatusCode . Unauthorized )
789
+ if ( fsResult == FileSystemStatusCode . Unauthorized || fsResult == FileSystemStatusCode . ReadOnly )
753
790
{
754
- // Try again with MoveFileFromApp
755
- fsResult = ( FilesystemResult ) NativeFileOperationsHelper . MoveFileFromApp ( source . Path , destination ) ;
791
+ fsResult = await PerformAdminOperation ( new ValueSet ( )
792
+ {
793
+ { "Arguments" , "FileOperation" } ,
794
+ { "fileop" , "MoveItem" } ,
795
+ { "filepath" , source . Path } ,
796
+ { "destpath" , destination } ,
797
+ { "overwrite" , false }
798
+ } ) ;
756
799
}
757
- errorCode ? . Report ( fsResult ) ;
758
800
}
759
801
760
802
if ( fsResult )
@@ -832,6 +874,27 @@ private static async Task<StorageFolder> MoveDirectoryAsync(IStorageFolder sourc
832
874
return createdRoot ;
833
875
}
834
876
877
+ private async Task < FilesystemResult > PerformAdminOperation ( ValueSet operation )
878
+ {
879
+ var elevateConfirmDialog = new Files . Dialogs . ElevateConfirmDialog ( ) ;
880
+ var elevateConfirmResult = await elevateConfirmDialog . ShowAsync ( ) ;
881
+ if ( elevateConfirmResult == ContentDialogResult . Primary )
882
+ {
883
+ if ( associatedInstance . ServiceConnection != null &&
884
+ await associatedInstance . ServiceConnection . Elevate ( ) )
885
+ {
886
+ // Try again with fulltrust process (admin)
887
+ if ( associatedInstance . ServiceConnection != null )
888
+ {
889
+ var ( status , response ) = await associatedInstance . ServiceConnection . SendMessageForResponseAsync ( operation ) ;
890
+ return ( FilesystemResult ) ( status == AppServiceResponseStatus . Success
891
+ && response . Get ( "Success" , false ) ) ;
892
+ }
893
+ }
894
+ }
895
+ return ( FilesystemResult ) false ;
896
+ }
897
+
835
898
#endregion Helpers
836
899
837
900
#region IDisposable
0 commit comments