Skip to content

Commit 6afa7d9

Browse files
authored
Enable app elevation when deleting files (#4203)
1 parent e8ba4b1 commit 6afa7d9

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

Files/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -576,25 +576,41 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source,
576576

577577
if (fsResult == FileSystemStatusCode.Unauthorized)
578578
{
579-
// Try again with fulltrust process
580-
var elevateConfirmDialog = new Files.Dialogs.ElevateConfirmDialog();
581-
var elevateConfirmResult = await elevateConfirmDialog.ShowAsync();
582-
if (elevateConfirmResult == ContentDialogResult.Primary)
579+
// Try again with fulltrust process (non admin: for shortcuts and hidden files)
580+
if (associatedInstance.ServiceConnection != null)
583581
{
584-
//await associatedInstance.ServiceConnection?.Elevate();
585-
App.InteractionViewModel.IsFullTrustElevated = true;
586-
587-
if (associatedInstance.ServiceConnection != null)
582+
var (status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(new ValueSet()
583+
{
584+
{ "Arguments", "FileOperation" },
585+
{ "fileop", "DeleteItem" },
586+
{ "filepath", source.Path },
587+
{ "permanently", permanently }
588+
});
589+
fsResult = (FilesystemResult)(status == AppServiceResponseStatus.Success
590+
&& response.Get("Success", false));
591+
}
592+
if (!fsResult)
593+
{
594+
var elevateConfirmDialog = new Files.Dialogs.ElevateConfirmDialog();
595+
var elevateConfirmResult = await elevateConfirmDialog.ShowAsync();
596+
if (elevateConfirmResult == ContentDialogResult.Primary)
588597
{
589-
var (status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(new ValueSet()
598+
if (await associatedInstance.ServiceConnection?.Elevate()) // TODO: enable this
590599
{
591-
{ "Arguments", "FileOperation" },
592-
{ "fileop", "DeleteItem" },
593-
{ "filepath", source.Path },
594-
{ "permanently", permanently }
595-
});
596-
fsResult = (FilesystemResult)(status == AppServiceResponseStatus.Success
597-
&& response.Get("Success", false));
600+
// Try again with fulltrust process (admin)
601+
if (associatedInstance.ServiceConnection != null)
602+
{
603+
var (status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(new ValueSet()
604+
{
605+
{ "Arguments", "FileOperation" },
606+
{ "fileop", "DeleteItem" },
607+
{ "filepath", source.Path },
608+
{ "permanently", permanently }
609+
});
610+
fsResult = (FilesystemResult)(status == AppServiceResponseStatus.Success
611+
&& response.Get("Success", false));
612+
}
613+
}
598614
}
599615
}
600616
}

Files/Helpers/AppServiceConnectionHelper.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,39 @@ public static async Task<bool> Elevate(this NamedPipeAsAppServiceConnection conn
5151
{
5252
if (connection == null)
5353
{
54+
App.InteractionViewModel.IsFullTrustElevated = false;
5455
return false;
5556
}
5657

58+
bool wasElevated = false;
59+
5760
var (status, response) = await connection.SendMessageForResponseAsync(new ValueSet() { { "Arguments", "Elevate" } });
5861
if (status == AppServiceResponseStatus.Success)
5962
{
6063
var res = response.Get("Success", 1L);
6164
switch (res)
6265
{
63-
case 0:
66+
case 0: // FTP is restarting as admin
6467
var nullConn = Task.FromResult<NamedPipeAsAppServiceConnection>(null);
6568
ConnectionChanged?.Invoke(null, nullConn);
6669
(await Instance)?.Dispose();
6770
Instance = BuildConnection(false); // Fulltrust process is already running
6871
_ = await Instance;
6972
ConnectionChanged?.Invoke(null, Instance);
73+
wasElevated = true;
74+
break;
75+
case -1: // FTP is already admin
76+
wasElevated = true;
77+
break;
78+
default: // Failed (e.g canceled UAC)
79+
wasElevated = false;
7080
break;
71-
72-
case -1:
73-
return true;
74-
75-
default:
76-
return false;
7781
}
7882
}
7983

80-
return false;
84+
App.InteractionViewModel.IsFullTrustElevated = wasElevated;
85+
86+
return wasElevated;
8187
}
8288

8389
private static async Task<NamedPipeAsAppServiceConnection> BuildConnection(bool launchFullTrust)

0 commit comments

Comments
 (0)