Exemplo n.º 1
0
        /// <summary>
        /// Gets a <see cref="KnownFolderNative"/> object from an <see cref="IdList"/>
        /// based PIDL if this represents a knownfolder, or otherwise, null.
        /// </summary>
        /// <param name="ashellListId"></param>
        /// <returns></returns>
        internal static KnownFolderNative FromPIDL(IdList ashellListId)
        {
            bool isDesktop = true;

            if (ashellListId != null)
            {
                if (ashellListId.Size > 0)
                {
                    isDesktop = false;
                }
            }

            if (isDesktop == true)
            {
                return(KnownFolderHelper.FromPath(KF_IID.ID_FOLDERID_Desktop, true));
            }

            IntPtr pidl = default(IntPtr);

            try
            {
                pidl = PidlManager.IdListToPidl(ashellListId);

                KnownFolderManagerClass knownFolderManager = new KnownFolderManagerClass();

                IKnownFolderNative iknownFolder;
                HRESULT            hr = knownFolderManager.FindFolderFromIDList(pidl, out iknownFolder);

                return((hr == HRESULT.S_OK) ? new KnownFolderNative(iknownFolder) : null);
            }
            finally
            {
                pidl = PidlManager.ILFree(pidl);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the native known folder object given a PID list or null
        /// if known folder is not available on the local system.
        /// </summary>
        /// <param name="pidl"></param>
        /// <returns></returns>
        internal static KnownFolderNative FromPIDL(IntPtr pidl)
        {
            KnownFolderManagerClass knownFolderManager = new KnownFolderManagerClass();

            IKnownFolderNative iknownFolder;
            HRESULT            hr = knownFolderManager.FindFolderFromIDList(pidl, out iknownFolder);

            return((hr == HRESULT.S_OK) ? new KnownFolderNative(iknownFolder) : null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns 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 KnownFolderNative FromKnownFolderGuid(Guid knownFolderId)
        {
            IKnownFolderNative      knownFolderNative;
            KnownFolderManagerClass knownFolderManager = new KnownFolderManagerClass();

            HRESULT hr = knownFolderManager.GetFolder(knownFolderId, out knownFolderNative);

            if (hr != HRESULT.S_OK)
            {
                return(null);
            }

            return(new KnownFolderNative(knownFolderNative));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a strongly-typed read-only collection of all the registered known folders.
        /// </summary>
        /// <returns></returns>
        public static Dictionary <Guid, IKnownFolderProperties> GetAllFolders()
        {
            // Should this method be thread-safe?? (It'll take a while
            // to get a list of all the known folders, create the managed wrapper
            // and return the read-only collection.
            var    foldersList = new Dictionary <Guid, IKnownFolderProperties>();
            var    pathList    = new Dictionary <string, IKnownFolderProperties>();
            uint   count;
            IntPtr folders = IntPtr.Zero;

            try
            {
                KnownFolderManagerClass knownFolderManager = new KnownFolderManagerClass();
                if (knownFolderManager.GetFolderIds(out folders, out count) != HRESULT.S_OK)
                {
                    return(foldersList);
                }

                if (count > 0 && folders != IntPtr.Zero)
                {
                    // Loop through all the KnownFolderID elements
                    for (int i = 0; i < count; i++)
                    {
                        // Read the current pointer
                        IntPtr current = new IntPtr(folders.ToInt64() + (Marshal.SizeOf(typeof(Guid)) * i));

                        // Convert to Guid
                        Guid knownFolderID = (Guid)Marshal.PtrToStructure(current, typeof(Guid));

                        try
                        {
                            using (var nativeKF = FromKnownFolderGuid(knownFolderID))
                            {
                                var kf = KnownFolderHelper.GetFolderProperties(nativeKF.Obj);

                                // Add to our collection if it's not null (some folders might not exist on the system
                                // or we could have an exception that resulted in the null return from above method call
                                if (kf != null)
                                {
                                    foldersList.Add(kf.FolderId, kf);

                                    if (kf.IsExistsInFileSystem == true)
                                    {
                                        IKnownFolderProperties obj;
                                        if (pathList.TryGetValue(kf.Path.ToUpper(), out obj))
                                        {
                                            pathList.Add(kf.Path.ToUpper(), kf);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                            // Catch this in case we have an exotic non-existing, non-access case
                        }
                    }
                }
            }
            finally
            {
                if (folders != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(folders);
                }
            }

            return(foldersList);
        }