/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Helpers/Win32/Win32Helper.Shell.cs
94 строки
3 KB
0x5bfa
Code Quality: Remove manual interop definitions and add agent instructions for this (#18449)
31 май 2026, 18:26
Не верифицирован
31 май 2026, 18:26
6245529
Код
Авторство
О чём код?
// 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; } } }