IShellItemExtensions.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.IO; 7 8 using ManagedCommon; 9 using Peek.Common.Models; 10 11 namespace Peek.UI.Extensions; 12 13 public static class IShellItemExtensions 14 { 15 public static IFileSystemItem ToIFileSystemItem(this IShellItem shellItem) 16 { 17 string path = shellItem.GetPath(); 18 string name = shellItem.GetName(); 19 20 return File.Exists(path) ? new FileItem(path, name) : new FolderItem(path, name, shellItem.GetParsingName()); 21 } 22 23 private static string GetPath(this IShellItem shellItem) => 24 shellItem.GetNameCore(Windows.Win32.UI.Shell.SIGDN.SIGDN_FILESYSPATH, logError: false); 25 26 private static string GetName(this IShellItem shellItem) => 27 shellItem.GetNameCore(Windows.Win32.UI.Shell.SIGDN.SIGDN_NORMALDISPLAY, logError: true); 28 29 private static string GetParsingName(this IShellItem shellItem) => 30 shellItem.GetNameCore(Windows.Win32.UI.Shell.SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, logError: true); 31 32 private static string GetNameCore(this IShellItem shellItem, Windows.Win32.UI.Shell.SIGDN displayNameType, bool logError) 33 { 34 try 35 { 36 return shellItem.GetDisplayName(displayNameType); 37 } 38 catch (Exception ex) 39 { 40 if (logError) 41 { 42 Logger.LogError($"Getting {Enum.GetName(displayNameType)} failed. {ex.Message}"); 43 } 44 45 return string.Empty; 46 } 47 } 48 }