forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWin32Helper.Shell.cs
More file actions
94 lines (82 loc) · 2.65 KB
/
Copy pathWin32Helper.Shell.cs
File metadata and controls
94 lines (82 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.
using System.IO;
using System.Runtime.InteropServices;
using Vanara.PInvoke;
using Vanara.Windows.Shell;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Shell;
namespace Files.App.Helpers
{
/// <summary>
/// Provides static helper for Win32.
/// </summary>
public static partial class Win32Helper
{
public static async Task<(ShellFileItem Folder, List<ShellFileItem> Enumerate)> GetShellFolderAsync(string path, bool getFolder, bool getEnumerate, int from, int count, params string[] properties)
{
if (path.StartsWith("::{", StringComparison.Ordinal))
{
path = $"shell:{path}";
}
return await STATask.Run(() =>
{
var flc = new List<ShellFileItem>();
var folder = (ShellFileItem)null;
try
{
using var shellFolder = ShellFolderExtensions.GetShellItemFromPathOrPIDL(path) as ShellFolder;
using ShellFolder _controlPanel = new(Shell32.KNOWNFOLDERID.FOLDERID_ControlPanelFolder);
using ShellFolder _controlPanelCategoryView = new("::{26EE0668-A00A-44D7-9371-BEB064C98683}");
if (shellFolder is null ||
(_controlPanel.PIDL.IsParentOf(shellFolder.PIDL, false) ||
_controlPanelCategoryView.PIDL.IsParentOf(shellFolder.PIDL, false)) &&
!shellFolder.Any())
{
// Return null to force open unsupported items in explorer
// only if inside control panel and folder appears empty
return (null, flc);
}
if (getFolder)
folder = ShellFolderExtensions.GetShellFileItem(shellFolder);
if (getEnumerate)
{
foreach (var folderItem in shellFolder.Skip(from).Take(count))
{
try
{
var shellFileItem = folderItem is ShellLink link ?
ShellFolderExtensions.GetShellLinkItem(link) :
ShellFolderExtensions.GetShellFileItem(folderItem);
foreach (var prop in properties)
shellFileItem.Properties[prop] = SafetyExtensions.IgnoreExceptions(() => folderItem.Properties[prop]);
flc.Add(shellFileItem);
}
catch (Exception ex) when (ex is FileNotFoundException || ex is DirectoryNotFoundException)
{
// Happens if files are being deleted
}
finally
{
folderItem.Dispose();
}
}
}
}
catch
{
}
return (folder, flc);
}, App.Logger);
}
public static unsafe string GetFolderFromKnownFolderGUID(Guid guid)
{
PWSTR pszPath;
PInvoke.SHGetKnownFolderPath(ref guid, (KNOWN_FOLDER_FLAG)0, null, out pszPath);
string path = pszPath.ToString();
Marshal.FreeCoTaskMem((nint)pszPath.Value);
return path;
}
}
}