Пример #1
0
        /// <summary>
        /// Gets the actual file system path (e.g.: 'C:\Windows') for a KnownfolderID.
        ///
        /// The <paramref name="knownfolderIdPath"/> is expected to follow the format
        /// in <seealso cref="KF_IID"/> definitions.
        /// </summary>
        /// <param name="knownfolderIdPath">Identifies the known folder in question</param>
        /// <returns>null if path does not exist or the actual path.</returns>
        public static string GetKnownFolderPath(string knownfolderIdPath)
        {
            // Get the KnownFolderId Guid for this special folder
            var kf_guid = new Guid(knownfolderIdPath.Substring(KF_IID.IID_Prefix.Length));

            try
            {
                using (var kf = KnownFolderHelper.FromKnownFolderGuid(kf_guid))
                {
                    if (kf != null)
                    {
                        return(kf.GetPath());
                    }
                }
            }
            catch
            {
                // Guard: in case the caller has passed an invalid id we return null.
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Returns the <see cref="KnownFolderNative"/> object that represents the desktop object.
        /// </summary>
        /// <returns></returns>
        public static KnownFolderNative GetDesktop()
        {
            IntPtr pidl = default(IntPtr);

            try
            {
                NativeMethods.SHGetKnownFolderIDList(KnownFolderGuids.FOLDERID_Desktop,
                                                     KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero, out pidl);

                if (pidl != default(IntPtr))
                {
                    return(KnownFolderHelper.FromPIDL(pidl));
                }
            }
            finally
            {
                if (pidl != default(IntPtr))
                {
                    NativeMethods.ILFree(pidl);
                }
            }

            return(null);
        }
Пример #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);
                    }
                }
            }
        }
Пример #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);
        }
Пример #5
0
        /// <summary>
        /// Returns a known folder given its shell path, such as <c>C:\users\public\documents</c> or
        /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
        /// </summary>
        /// <param name="path">The path for the requested known folder; either a physical path or a virtual path.</param>
        /// <param name="IsSpecialPath"></param>
        /// <returns>A known folder representing the specified name.</returns>
        public static KnownFolderNative FromPath(string path, bool?IsSpecialPath)
        {
            if (string.IsNullOrEmpty(path) == true)
            {
                throw new ArgumentNullException("'path' parameter cannot be Empty or Null.");
            }

            if (IsSpecialPath == null)
            {
                IsSpecialPath = (ShellHelpers.IsSpecialPath(path) == ShellHelpers.SpecialPath.IsSpecialPath);
            }

            if (IsSpecialPath == true)
            {
                // Get the KnownFolderId Guid for this special folder
                var kf_guid = new Guid(path.Substring(KF_IID.IID_Prefix.Length));

                try
                {
                    var ret = FromKnownFolderGuid(kf_guid);

                    if (ret != null)
                    {
                        return(ret);
                    }
                }
                catch
                {
                    // continue iteration on exceptional errors
                }
            }

            IntPtr pidl = default(IntPtr);

            try
            {
                pidl = ShellHelpers.PIDLFromPath(path);

                if (pidl == default(IntPtr))
                {
                    // try one more time with a trailing \0
                    pidl = ShellHelpers.PidlFromParsingName(path);
                }

                if (pidl != default(IntPtr))
                {
                    // It's probably a special folder, try to get it
                    var kf = KnownFolderHelper.FromPIDL(pidl);

                    if (kf != null)
                    {
                        return(kf);
                    }
                }
            }
            finally
            {
                pidl = PidlManager.ILFree(pidl);
            }

            return(null);
        }