Example #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);
        }
Example #2
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);
        }
Example #3
0
        private void Init(IKnownFolderNative knownFolderNative,
                          NativeFolderDefinition nativeFolderDefinition)
        {
            this.Name = Marshal.PtrToStringUni(nativeFolderDefinition.name);

            this.Category                = nativeFolderDefinition.category;
            this.CanonicalName           = Marshal.PtrToStringUni(nativeFolderDefinition.name);
            this.Description             = Marshal.PtrToStringUni(nativeFolderDefinition.description);
            this.ParentId                = nativeFolderDefinition.parentId;
            this.RelativePath            = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath);
            this.ParsingName             = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName);
            this.TooltipResourceId       = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip);
            this.LocalizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName);
            this.IconResourceId          = Marshal.PtrToStringUni(nativeFolderDefinition.icon);
            this.Security                = Marshal.PtrToStringUni(nativeFolderDefinition.security);
            this.FileAttributes          = (System.IO.FileAttributes)nativeFolderDefinition.attributes;
            this.DefinitionOptions       = nativeFolderDefinition.definitionOptions;
            this.FolderTypeId            = nativeFolderDefinition.folderTypeId;
            ////knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId);

            this.Redirection = knownFolderNative.GetRedirectionCapabilities();

            // Turn tooltip, localized name and icon resource IDs
            // into the actual resources.
            this.Tooltip       = GetStringResource(this.TooltipResourceId);
            this.LocalizedName = GetStringResource(this.LocalizedNameResourceId);

            this.FolderId = knownFolderNative.GetId();

            bool pathExists = false;

            this.IsExistsInFileSystem = false;
            this.IsPathExists         = false;
            this.PidlIdList           = null;

            using (var kfObj = new KnownFolderNative(knownFolderNative))
            {
                if (kfObj != null)
                {
                    try
                    {
                        bool?isExists = kfObj.IsFileSystem();

                        if (isExists != null)
                        {
                            if (isExists == true)
                            {
                                this.IsExistsInFileSystem = true;
                            }

                            this.Path         = KnownFolderHelper.GetPath(out pathExists, knownFolderNative);
                            this.IsPathExists = pathExists;
                        }
                    }
                    catch
                    {
                        // Catch this just in case
                    }

                    try
                    {
                        this.PidlIdList = kfObj.KnownFolderToIdList();
                    }
                    catch
                    {
                        // Catch this just in case
                    }
                }
            }

            // Load Icon ResourceId from Icon Resource helper (if not already present)
            if (IsIconResourceIdValid(IconResourceId) == false && PidlIdList != null)
            {
                if (PidlIdList.Size == 0 && this.FolderId == new Guid(KF_ID.ID_FOLDERID_Desktop))
                {
                    IconResourceId = IconHelper.FromPidl(PidlIdList, true, false);
                }
                else
                {
                    if (PidlIdList.Size != 0)
                    {
                        IconResourceId = IconHelper.FromPidl(PidlIdList, true, false);
                    }
                }
            }
        }