Ejemplo n.º 1
0
        /// <summary>
        /// Converts a value to another type.
        /// </summary>
        /// <param name="value">
        /// The value to convert.
        /// </param>
        /// <param name="targetType">
        /// The type to convert to.
        /// </param>
        /// <param name="parameter">
        /// An optional parameter.
        /// </param>
        /// <param name="culture">
        /// The UI culture to convert to.
        /// </param>
        /// <returns>
        /// The converted value.
        /// </returns>
        public object?Convert(object?value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(string))
            {
                throw new InvalidOperationException("Cannot convert to any type except System.String");
            }

            if (value == null)
            {
                return(FallbackValue);
            }

            var path = (string)value;

            if (!Path.IsPathRooted(path))
            {
                throw new ArgumentException("Path must be rooted", nameof(value));
            }

            Shell32.IShellItem2 item = Shell32.SHCreateItemFromParsingName <Shell32.IShellItem2>(path);

            Shell32.SIGDN sigdn = DisplayMode switch
            {
                FileNameDisplayMode.Default => Shell32.SIGDN.SIGDN_NORMALDISPLAY,
                FileNameDisplayMode.FullPath => Shell32.SIGDN.SIGDN_FILESYSPATH,
                FileNameDisplayMode.NameOnly => Shell32.SIGDN.SIGDN_PARENTRELATIVEPARSING,
                _ => throw new InvalidOperationException($"Unexpected {nameof(FileNameDisplayMode)}")
            };

            return(item.GetDisplayName(sigdn));
        }
            HRESULT Shell32.IShellItem.GetDisplayName(Shell32.SIGDN sigdnName, out string?ppszName)
            {
                IntPtr  ppszName_local;
                HRESULT retVal;

                retVal = ((delegate * unmanaged <IntPtr, int, IntPtr *, HRESULT>)(*(*(void ***)_wrappedInstance + 5)))
                             (_wrappedInstance, (int)sigdnName, &ppszName_local);
                ppszName = Marshal.PtrToStringUni(ppszName_local);
                Marshal.FreeCoTaskMem(ppszName_local);
                return(retVal);
            }