Skip to content

Commit 5046763

Browse files
authored
Feature: Display prompt when user doesn't have permission to access a folder (#10174)
1 parent 3548094 commit 5046763

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,6 +2841,9 @@
28412841
<data name="DeleteShortcutDescription" xml:space="preserve">
28422842
<value>The target destination cannot be found {0}. Do you want to delete this shortcut?</value>
28432843
</data>
2844+
<data name="AccessDeniedToFolder" xml:space="preserve">
2845+
<value>You don't have permission to access this folder.</value>
2846+
</data>
28442847
<data name="LoadingThemes" xml:space="preserve">
28452848
<value>Loading Themes</value>
28462849
</data>

src/Files.App/ViewModels/ItemViewModel.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
using System.Linq;
3535
using System.Net;
3636
using System.Runtime.CompilerServices;
37+
using System.Runtime.InteropServices;
3738
using System.Text;
3839
using System.Text.Json;
3940
using System.Threading;
@@ -1478,10 +1479,9 @@ public async Task<int> EnumerateItemsFromStandardFolderAsync(string path, Cancel
14781479
}
14791480
else if (res == FileSystemStatusCode.Unauthorized)
14801481
{
1481-
//TODO: proper dialog
14821482
await DialogDisplayHelper.ShowDialogAsync(
14831483
"AccessDenied".GetLocalizedResource(),
1484-
"SubDirectoryAccessDenied".GetLocalizedResource());
1484+
"AccessDeniedToFolder".GetLocalizedResource());
14851485
return -1;
14861486
}
14871487
else if (res == FileSystemStatusCode.NotFound)
@@ -1531,13 +1531,13 @@ await DialogDisplayHelper.ShowDialogAsync(
15311531
}
15321532
else
15331533
{
1534-
(IntPtr hFile, WIN32_FIND_DATA findData) = await Task.Run(() =>
1534+
(IntPtr hFile, WIN32_FIND_DATA findData, int errorCode) = await Task.Run(() =>
15351535
{
15361536
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
15371537
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
15381538
IntPtr hFileTsk = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findDataTsk, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
15391539
additionalFlags);
1540-
return (hFileTsk, findDataTsk);
1540+
return (hFileTsk, findDataTsk, hFileTsk.ToInt64() == -1 ? Marshal.GetLastWin32Error() : 0);
15411541
}).WithTimeoutAsync(TimeSpan.FromSeconds(5));
15421542

15431543
var itemModifiedDate = DateTime.Now;
@@ -1584,6 +1584,17 @@ await DialogDisplayHelper.ShowDialogAsync(
15841584
else if (hFile.ToInt64() == -1)
15851585
{
15861586
await EnumFromStorageFolderAsync(path, currentFolder, rootFolder, currentStorageFolder, cancellationToken);
1587+
if (!filesAndFolders.Any())
1588+
{
1589+
// https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
1590+
if (errorCode == 0x5) // ERROR_ACCESS_DENIED
1591+
{
1592+
await DialogDisplayHelper.ShowDialogAsync(
1593+
"AccessDenied".GetLocalizedResource(),
1594+
"AccessDeniedToFolder".GetLocalizedResource());
1595+
return -1;
1596+
}
1597+
}
15871598
return 1;
15881599
}
15891600
else

0 commit comments

Comments
 (0)