Пример #1
0
        private void GetImageReference()
        {
            IPropertyStore store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject);

            using (PropVariant propVar = new PropVariant())
            {
                store.GetValue(ref propertyKey, propVar);

                Marshal.ReleaseComObject(store);
                store = null;

                string refPath;
                ((IPropertyDescription2)Description.NativePropertyDescription).GetImageReferenceForValue(
                    propVar, out refPath);

                if (refPath == null)
                {
                    return;
                }

                int index = ShellNativeMethods.PathParseIconLocation(ref refPath);
                if (refPath != null)
                {
                    imageReferencePath      = refPath;
                    imageReferenceIconIndex = index;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns a formatted, Unicode string representation of a property value.
        /// </summary>
        /// <param name="format">One or more of the PropertyDescriptionFormat flags
        /// that indicate the desired format.</param>
        /// <returns>The formatted value as a string, or null if this property
        /// cannot be formatted for display.</returns>
        public string FormatForDisplay(PropertyDescriptionFormatOptions format)
        {
            string formattedString;

            if (Description == null || Description.NativePropertyDescription == null)
            {
                // We cannot do anything without a property description
                return(null);
            }

            IPropertyStore store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject);

            using (PropVariant propVar = new PropVariant())
            {
                store.GetValue(ref propertyKey, propVar);

                // Release the Propertystore
                Marshal.ReleaseComObject(store);
                store = null;

                HResult hr = Description.NativePropertyDescription.FormatForDisplay(propVar, ref format, out formattedString);

                // Sometimes, the value cannot be displayed properly, such as for blobs
                // or if we get argument exception
                if (!CoreErrorHelper.Succeeded(hr))
                {
                    throw new ShellException(hr);
                }

                return(formattedString);
            }
        }
        /// <summary>Returns a formatted, Unicode string representation of a property value.</summary>
        /// <param name="format">One or more of the PropertyDescriptionFormat flags that indicate the desired format.</param>
        /// <param name="formattedString">The formatted value as a string, or null if this property cannot be formatted for display.</param>
        /// <returns>True if the method successfully locates the formatted string; otherwise False.</returns>
        public bool TryFormatForDisplay(PropertyDescriptionFormatOptions format, out string formattedString)
        {
            if (Description == null || Description.NativePropertyDescription == null)
            {
                // We cannot do anything without a property description
                formattedString = null;
                return(false);
            }

            var store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject);

            using (var propVar = new PropVariant())
            {
                store.GetValue(ref propertyKey, propVar);

                // Release the Propertystore
                Marshal.ReleaseComObject(store);
                store = null;

                var hr = Description.NativePropertyDescription.FormatForDisplay(propVar, ref format, out formattedString);

                // Sometimes, the value cannot be displayed properly, such as for blobs or if we get argument exception
                if (!CoreErrorHelper.Succeeded(hr))
                {
                    formattedString = null;
                    return(false);
                }
                return(true);
            }
        }
        private void GetImageReference()
        {
            IPropertyStore store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject);

#if CS7

            using (var propVar = new PropVariant())

            {

#else

            using var propVar = new PropVariant();

#endif

            _ = store.GetValue(ref propertyKey, propVar);

            _ = Marshal.ReleaseComObject(store);
            store = null;

            ((IPropertyDescription2)Description.NativePropertyDescription).GetImageReferenceForValue(
                propVar, out string refPath);

            if (refPath == null) return;

            int index = Win32Native.Shell.Shell.PathParseIconLocation(ref refPath);

            if (refPath != null)
            {
                imageReferencePath = refPath;
                imageReferenceIconIndex = index;
            }

#if CS7

            }

#endif

        }