Exemple #1
0
        /// <summary>
        /// Gets the value from the specified <see cref="PropertyKey"/>
        /// </summary>
        /// <param name="key">The property being requested</param>
        /// <returns>The value of the specified property</returns>
        public PropVariant GetProperty(PropertyKey key)
        {
            IntPtr ppv;

            _pItem.GetProperty(key.MarshalledPointer, out ppv);
            PropVariant v = new PropVariant(ppv);

            Marshal.FreeCoTaskMem(ppv);
            return(v);
        }
        private static string GetPrinterFriendlyNameIfPrinter(IShellItem2 shellItem)
        {
            // Devices_PrimaryCategory returns "Printers" for printers and faxes on Windows 10 but "Printers and faxes" on Windows 7.
            using (var categoryIds = new PropVariantSafeHandle())
            {
                shellItem.GetProperty(PKEY.Devices_CategoryIds, categoryIds).ThrowIfError();
                if (!categoryIds.ToStringVector().Any(id => id.StartsWith("PrintFax", StringComparison.OrdinalIgnoreCase)))
                {
                    return(null);
                }
            }

            // The canonical or "friendly name" needed to match the devmode
            // https://blogs.msdn.microsoft.com/asklar/2009/10/21/getting-the-printer-friendly-name-from-the-device-center-shell-folder/
            // PKEY_Devices_InterfacePaths doesn't seem to ever be found, but PKEY_Devices_FriendlyName works so...
            shellItem.GetString(PKEY.Devices_FriendlyName, out var friendlyName).ThrowIfError();
            return(friendlyName.ReadAndFree());
        }