Skip to content

Commit 97df461

Browse files
authored
Fix: Fixed crash that would occur when switching between Network and Recycle Bin (#10728)
1 parent ce29bda commit 97df461

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/Files.App/Filesystem/RecentItem.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CommunityToolkit.Mvvm.ComponentModel;
2+
using Files.App.Filesystem.StorageItems;
23
using Files.App.Helpers;
34
using Files.Shared;
45
using Microsoft.UI.Xaml.Media.Imaging;
@@ -63,8 +64,8 @@ public RecentItem(ShellLinkItem linkItem) : base()
6364
/// <param name="fileItem">The shell file item</param>
6465
public RecentItem(ShellFileItem fileItem) : base()
6566
{
66-
LinkPath = fileItem.FilePath; // intentionally the same
67-
RecentPath = fileItem.FilePath; // intentionally the same
67+
LinkPath = ShellStorageFolder.IsShellPath(fileItem.FilePath) ? fileItem.RecyclePath : fileItem.FilePath; // use true path on disk for shell items
68+
RecentPath = LinkPath; // intentionally the same
6869
Name = NameOrPathWithoutExtension(fileItem.FileName);
6970
Type = fileItem.IsFolder ? StorageItemTypes.Folder : StorageItemTypes.File;
7071
FolderImg = fileItem.IsFolder;

src/Files.App/Filesystem/StorageItems/ShellStorageFile.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Files.App.Shell;
22
using Files.Shared;
3+
using Files.Shared.Extensions;
34
using System;
45
using System.Runtime.InteropServices.WindowsRuntime;
56
using System.Threading.Tasks;
@@ -85,10 +86,18 @@ public static IAsyncOperation<BaseStorageFile> FromPathAsync(string path)
8586
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation();
8687
}
8788

88-
private static ShellFileItem GetFile(string path)
89+
private static ShellFileItem? GetFile(string path)
8990
{
90-
using var shellItem = ShellFolderExtensions.GetShellItemFromPathOrPidl(path);
91-
return ShellFolderExtensions.GetShellFileItem(shellItem);
91+
try
92+
{
93+
using var shellItem = ShellFolderExtensions.GetShellItemFromPathOrPidl(path);
94+
return ShellFolderExtensions.GetShellFileItem(shellItem);
95+
}
96+
catch
97+
{
98+
// Can happen when dealing with recent items or when browsing shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}
99+
return default;
100+
}
92101
}
93102

94103
public override bool IsEqual(IStorageItem item) => item?.Path == Path;

src/Files.App/Shell/ShellFolderExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static ShellFileItem GetShellFileItem(ShellItem folderItem)
5353
"::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}" => "Shell:NetworkPlacesFolder",
5454
"::{208D2C60-3AEA-1069-A2D7-08002B30309D}" => "Shell:NetworkPlacesFolder",
5555
// Use PIDL as path
56-
_ => $@"\\SHELL\{string.Join("\\", folderItem.PIDL.Select(x => x.GetBytes()).Select(x => Convert.ToBase64String(x, 0, x.Length)))}"
56+
// Replace "/" with "_" to avoid confusion with path separator
57+
_ => $@"\\SHELL\{string.Join("\\", folderItem.PIDL.Select(x => x.GetBytes()).Select(x => Convert.ToBase64String(x, 0, x.Length).Replace("/", "_")))}"
5758
};
5859
}
5960
var fileName = folderItem.Properties.TryGetProperty<string>(Ole32.PROPERTYKEY.System.ItemNameDisplay);
@@ -122,8 +123,9 @@ public static bool GetStringAsPidl(string pathOrPidl, out Shell32.PIDL pidl)
122123
if (pathOrPidl.StartsWith(@"\\SHELL\", StringComparison.Ordinal))
123124
{
124125
pidl = pathOrPidl.Replace(@"\\SHELL\", "", StringComparison.Ordinal)
126+
.Replace("_", "/") // Avoid confusion with path separator
125127
.Split('\\', StringSplitOptions.RemoveEmptyEntries)
126-
.Select(pathSegment => new Shell32.PIDL(pathSegment))
128+
.Select(pathSegment => new Shell32.PIDL(Convert.FromBase64String(pathSegment)))
127129
.Aggregate((x, y) => Shell32.PIDL.Combine(x, y));
128130
return true;
129131
}

0 commit comments

Comments
 (0)