Beispiel #1
0
        /// <summary>
        ///     Create a new instance of the <see cref="ShellKnownFolder" /> class
        ///     to the specified parsing name.
        /// </summary>
        /// <param name="parsingName">Parsing name or path.</param>
        /// <returns><see cref="ShellKnownFolder" />.</returns>
        /// <exception cref="ArgumentException">
        ///     A known folder matching the specified standard folder name or path could not be
        ///     created.
        /// </exception>
        public static ShellKnownFolder FromParsingName(string parsingName)
        {
            Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(parsingName));
            Contract.Ensures(Contract.Result <ShellKnownFolder>() != null);

            var pidl  = PIDL.Null;
            var pidl2 = PIDL.Null;

            try
            {
                pidl = PIDL.FromParsingName(parsingName);
                if (pidl.IsNull)
                {
                    throw new ArgumentException(ErrorMessages.KnownFolderParsingName, nameof(parsingName));
                }

                var knownFolderInterface = FromPIDL(pidl);
                if (knownFolderInterface != null)
                {
                    var kf = CreateKnownFolder(knownFolderInterface);
                    if (kf == null)
                    {
                        throw new ArgumentException(ErrorMessages.KnownFolderParsingName, nameof(parsingName));
                    }
                    return(kf);
                }

                pidl2 = PIDL.FromParsingName(parsingName.PadRight(1, '\0'));
                if (pidl2.IsNull)
                {
                    throw new ArgumentException(ErrorMessages.KnownFolderParsingName, nameof(parsingName));
                }

                var knownFolder = FromPIDL(pidl2);
                if (knownFolder != null)
                {
                    var result = CreateKnownFolder(knownFolder);
                    if (result == null)
                    {
                        throw new ArgumentException(ErrorMessages.KnownFolderParsingName, nameof(parsingName));
                    }
                    return(result);
                }

                throw new ArgumentException(ErrorMessages.KnownFolderParsingName, nameof(parsingName));
            }
            finally
            {
                pidl.Free();
                pidl2.Free();
            }
        }