Esempio n. 1
0
        /// <summary>
        /// Method attempts to find that parent of a known folder by looking at the
        /// ParentId field in the associated <see cref="KnownFolderProperties"/> object
        /// and returns the new parent and child <see cref="IdList"/>s on success.
        /// </summary>
        /// <param name="ashellListId"></param>
        /// <param name="parentList"></param>
        /// <param name="relativeChild"></param>
        /// <returns>True if known folder parent was available in ParentId field,
        /// otherwise false.</returns>
        public static bool GetParentChildIdList(IdList ashellListId
                                                , out IdList parentList
                                                , out IdList relativeChild)
        {
            parentList    = null;
            relativeChild = null;

            relativeChild = ashellListId.GetRelativeChildId();
            parentList    = ashellListId.GetParentId();

            // Get Parent Id which is always the first part minus last id in the sequence
            if (parentList != null)
            {
                return(true);
            }

            // Try to find parent through known folder information lookup
            using (KnownFolderNative kf = KnownFolderHelper.FromPIDL(ashellListId))
            {
                if (kf != null)
                {
                    var props = KnownFolderHelper.GetFolderProperties(kf.Obj);

                    if (props.Parent != null)
                    {
                        if ((parentList = IdList.FromKnownFolderGuid(props.ParentId)) != null)
                        {
                            return(true);
                        }
                    }
                }
            }

            // Just return the desktop as parent if the given item has no more parents
            using (var desktop = KnownFolderHelper.FromKnownFolderGuid(new Guid(KF_ID.ID_FOLDERID_Desktop)))
            {
                IntPtr desktopPtr = default(IntPtr);
                try
                {
                    desktopPtr = desktop.KnownFolderToPIDL();

                    if (desktopPtr != default(IntPtr))
                    {
                        parentList = IdList.Create(PidlManager.Decode(desktopPtr));
                        return(true);
                    }
                }
                finally
                {
                    if (desktopPtr != default(IntPtr))
                    {
                        desktopPtr = PidlManager.ILFree(desktopPtr);
                    }
                }
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the parent IdList (parent PIDL) if available and a
        /// relative child idList (relative child PIDL).
        /// </summary>
        /// <param name="originalPath"></param>
        /// <param name="parentList"></param>
        /// <param name="relativeChild"></param>
        /// <returns></returns>
        public static bool GetParentIdListFromPath(string originalPath
                                                   , out IdList parentList
                                                   , out IdList relativeChild)
        {
            parentList    = null;
            relativeChild = null;

            if (string.IsNullOrEmpty(originalPath))
            {
                return(false);
            }

            // Desktop is the root of the shell and does not have a parent
            if (originalPath.Equals(KF_IID.ID_FOLDERID_Desktop, StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            IntPtr apidl = default(IntPtr);

            try
            {
                // Decode the pidl for this path into a list of ShellIds
                apidl = PidlManager.GetPIDLFromPath(originalPath);

                if (apidl == default(IntPtr)) // Cannot resolve this PIDL
                {
                    return(false);
                }

                // Convert PIDL into list of shellids and remove last id
                var ashellListId = PidlManager.Decode(apidl);

                return(GetParentChildIdList(IdList.Create(ashellListId),
                                            out parentList, out relativeChild));
            }
            finally
            {
                apidl = PidlManager.ILFree(apidl);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns an <see cref="IdList"/> (PIDL) for a known folder given a
        /// globally unique identifier.
        /// </summary>
        /// <param name="knownfolderId">A GUID for the requested known folder.</param>
        /// <returns>A known folder representing the specified name.</returns>
        /// <exception cref="System.ArgumentException">Thrown if the given Known Folder ID is invalid.</exception>
        public static IdList FromKnownFolderGuid(Guid knownfolderId)
        {
            using (KnownFolderNative KF = KnownFolderHelper.FromKnownFolderGuid(knownfolderId))
            {
                if (KF != null)
                {
                    IntPtr parentPidl = default(IntPtr);
                    try
                    {
                        parentPidl = KF.KnownFolderToPIDL();

                        // Convert PIDL into list of shellids and remove last id
                        return(IdList.Create(PidlManager.Decode(parentPidl)));
                    }
                    finally
                    {
                        parentPidl = PidlManager.ILFree(parentPidl);
                    }
                }
            }

            return(null);
        }