Exemple #1
0
        public void GetPidlForSpecialFolderPath()
        {
            string originalPath = null;
            IntPtr pidl = default(IntPtr), ptrPath = default(IntPtr);

            try
            {
                using (var kfn = KnownFolderHelper.FromKnownFolderGuid(new Guid(KF_ID.ID_FOLDERID_Windows)))
                {
                    pidl = kfn.KnownFolderToPIDL();
                    kfn.Obj.GetPath(0, out ptrPath);

                    Assert.IsTrue(ptrPath != default(IntPtr));

                    originalPath = Marshal.PtrToStringUni(ptrPath);
                }

                Assert.IsFalse(string.IsNullOrEmpty(originalPath));

                string logicalPath   = PidlManager.GetPathFromPIDL(pidl);
                string physStorePath = PidlManager.GetPathFromPIDL(pidl, TypOfPath.PhysicalStoragePath);

                Assert.IsTrue(string.Equals(originalPath, physStorePath, StringComparison.InvariantCultureIgnoreCase));
                Assert.IsFalse(string.Equals(logicalPath, physStorePath));

                ////Console.WriteLine("Path Retrieval via PIDL:'{0}' ->\nLogical Path: '{1}', Physical Path: '{2}'",
                ////    originalPath, logicalPath, physStorePath);
            }
            finally
            {
                pidl    = PidlManager.ILFree(pidl);
                ptrPath = PidlManager.FreeCoTaskMem(ptrPath);
            }
        }
Exemple #2
0
        public void GetPidlForDrivePath()
        {
            IntPtr pidl = default(IntPtr);

            try
            {
                // Get the default drive's path
                var drive = new DirectoryInfo(Environment.SystemDirectory).Root.Name;

                Assert.IsFalse(string.IsNullOrEmpty(drive));

                pidl = PidlManager.GetPIDLFromPath(drive);

                Assert.IsFalse(pidl == default(IntPtr));

                // Logical and physical path representation should be the same for drives
                string logicalPath   = PidlManager.GetPathFromPIDL(pidl);
                string physStorePath = PidlManager.GetPathFromPIDL(pidl, TypOfPath.PhysicalStoragePath);

                Assert.IsTrue(string.Equals(drive, physStorePath, StringComparison.CurrentCulture));
                Assert.IsTrue(string.Equals(logicalPath, physStorePath, StringComparison.CurrentCulture));

                ////Console.WriteLine("Path Retrieval via PIDL: '{0}' -> Logical Path: '{1}', Physical Path: '{2}'",
                ////                  drive, logicalPath, physStorePath);
            }
            finally
            {
                pidl = PidlManager.ILFree(pidl);
            }
        }
Exemple #3
0
        /// <summary>
        /// Gets the ResourceId (libararyName, index) of a shell icon
        /// based on an <see cref="IdList"/> object
        /// to support IconExtaction and display in UI layer.
        /// </summary>
        /// <param name="ilPidl"></param>
        /// <param name="isDirectory"></param>
        /// <param name="forceLoadFromDisk"></param>
        /// <param name="size"></param>
        /// <param name="iconState"></param>
        /// <returns></returns>
        public static string FromPidl(IdList ilPidl,
                                      bool isDirectory,
                                      bool forceLoadFromDisk,
                                      IconSize size = IconSize.large,
                                      ShellIconStateConstants iconState = ShellIconStateConstants.ShellIconStateNormal)
        {
            IntPtr ptrPidl = default(IntPtr);

            try
            {
                if ((ptrPidl = PidlManager.IdListToPidl(ilPidl)) != default(IntPtr))
                {
                    return(IconHelper.FromPidl(ptrPidl, isDirectory, forceLoadFromDisk,
                                               size, iconState));
                }
            }
            finally
            {
                if (ptrPidl != default(IntPtr))
                {
                    ptrPidl = PidlManager.ILFree(ptrPidl);
                }
            }

            return(null);
        }
Exemple #4
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);
            }
        }
Exemple #5
0
        public void GetPidlForDirectoryPath()
        {
            // Get the default drive's path
            var drive = new DirectoryInfo(Environment.SystemDirectory).Root.Name;

            Assert.IsFalse(string.IsNullOrEmpty(drive));

            // enumerate on root directores of the default drive and
            // verify correct pidl <-> path reprentation for each directory
            foreach (var originalPath in Directory.EnumerateDirectories(drive))
            {
                IntPtr pidl = default(IntPtr);
                try
                {
                    pidl = PidlManager.GetPIDLFromPath(originalPath);

                    Assert.IsFalse(pidl == default(IntPtr));

                    string logicalPath   = PidlManager.GetPathFromPIDL(pidl);
                    string physStorePath = PidlManager.GetPathFromPIDL(pidl, TypOfPath.PhysicalStoragePath);

                    // The logical path of a special path '::{...}' will differ from
                    // its physical representation 'C:\Windows'
                    // Otherwise, both repesentations should be equal for non-special folders
                    if (ShellHelpers.IsSpecialPath(logicalPath) == ShellHelpers.SpecialPath.IsSpecialPath)
                    {
                        Assert.IsFalse(string.Equals(logicalPath, physStorePath));
                    }
                    else
                    {
                        Assert.IsTrue(string.Equals(logicalPath, physStorePath));
                    }

                    ////Console.WriteLine("Path Retrieval via PIDL: '{0}' -> Logical Path: '{1}', Physical Path: '{2}'",
                    ////    originalPath, logicalPath, physStorePath);
                }
                finally
                {
                    pidl = PidlManager.ILFree(pidl);
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Calls the GetIDList method in the wrapped IKnownFolder interface to retrieve the ItemIdList
        /// for this KnownFolderItem.
        /// </summary>
        /// <returns>Type: PIDLIST_ABSOLUTE*
        /// When this method returns, contains the address of an absolute PIDL
        /// in its equivalent ItemIdList form.</returns>
        public IdList KnownFolderToIdList()
        {
            if (Obj == null)
            {
                throw new System.ArgumentException("Native IKnownFolder cannot be null.");
            }

            IntPtr ptrFullPidl = default(IntPtr);

            try
            {
                ptrFullPidl = KnownFolderToPIDL();

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

                return(IdList.Create(shellListIds));
            }
            finally
            {
                ptrFullPidl = PidlManager.ILFree(ptrFullPidl);
            }
        }
Exemple #7
0
////        public void LoadProperties()
////        {
////            if (_KnownFolderIsInitialized == false)
////            {
////                _KnownFolderIsInitialized = true;
////                KnownFolder = LoadKnownFolder();
////            }
////
////            if (_ItemTypeIsInitialized == false)
////            {
////                _ItemTypeIsInitialized = true;
////                ItemType = LoadItemType();
////            }
////
////            if (_IconResourceIdInitialized == false)
////            {
////                _IconResourceIdInitialized = true;
////                _IconResourceId = LoadIconResourceId();
////            }
////        }

        /// <summary>
        /// Initializes the items type flags and path properties.
        /// </summary>
        /// <param name="path">Is either a path reference a la 'C:' or a
        /// special folder path reference a la '::{...}' <seealso cref="KF_IID"/>
        /// for more details.</param>
        /// <returns>Returns a simple pojo type object to initialize
        /// the calling object members.</returns>
        internal static BrowseItemFromPath InitItem(string path)
        {
            if (string.IsNullOrEmpty(path) == true)   // return unknown references
            {
                var ret = new BrowseItemFromPath(path, path);
                ret.Name = path;
                return(ret);
            }

            if (path.Length == 38)
            {
                try
                {
                    Guid theGuid;
                    if (Guid.TryParse(path, out theGuid) == true)
                    {
                        path = KF_IID.IID_Prefix + path;
                    }
                }
                catch
                {
                    // Catching errors just in case ...
                }
            }

            // Return item for root desktop item
            if (string.Compare(path, KF_IID.ID_ROOT_Desktop, true) == 0)
            {
                return(InitDesktopRootItem());
            }

            ShellHelpers.SpecialPath isSpecialID = ShellHelpers.IsSpecialPath(path);
            string normPath = null, SpecialPathId = null;
            bool   hasPIDL = false;
            IdList parentIdList, relativeChildIdList;

            if (isSpecialID == ShellHelpers.SpecialPath.IsSpecialPath)
            {
                SpecialPathId = path;
                hasPIDL       = PidlManager.GetParentIdListFromPath(path, out parentIdList, out relativeChildIdList);
            }
            else
            {
                normPath = Browser.NormalizePath(path);
                hasPIDL  = PidlManager.GetParentIdListFromPath(normPath, out parentIdList, out relativeChildIdList);
            }

            if (hasPIDL == false)   // return references that cannot resolve with a PIDL
            {
                var ret = new BrowseItemFromPath(path, path);
                ret.Name = path;
                return(ret);
            }

            string parseName      = normPath;
            string name           = normPath;
            string labelName      = null;
            IdList fullIdList     = null;

            // Get the IShellFolder2 Interface for the original path item...
            IntPtr fullPidlPtr    = default(IntPtr);
            IntPtr ptrShellFolder = default(IntPtr);
            IntPtr parentPIDLPtr  = default(IntPtr);
            IntPtr relativeChildPIDLPtr = default(IntPtr);

            try
            {
                // We are asked to build the desktop root item here...
                if (parentIdList == null && relativeChildIdList == null)
                {
                    using (var shellFolder = new ShellFolderDesktop())
                    {
                        parseName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_FORPARSING);
                        name      = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_INFOLDER | SHGDNF.SHGDN_FOREDITING);
                        labelName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_NORMAL);
                    }
                }
                else
                {
                    fullIdList  = PidlManager.CombineParentChild(parentIdList, relativeChildIdList);
                    fullPidlPtr = PidlManager.IdListToPidl(fullIdList);

                    if (fullPidlPtr == default(IntPtr))
                    {
                        return(null);
                    }

                    HRESULT hr = HRESULT.False;

                    if (fullIdList.Size == 1) // Is this item directly under the desktop root?
                    {
                        hr = NativeMethods.SHGetDesktopFolder(out ptrShellFolder);

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

                        using (var shellFolder = new ShellFolder(ptrShellFolder))
                        {
                            parseName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_FORPARSING);
                            name      = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_INFOLDER | SHGDNF.SHGDN_FOREDITING);
                            labelName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_NORMAL);
                        }
                    }
                    else
                    {
                        parentPIDLPtr        = PidlManager.IdListToPidl(parentIdList);
                        relativeChildPIDLPtr = PidlManager.IdListToPidl(relativeChildIdList);

                        using (var desktopFolder = new ShellFolderDesktop())
                        {
                            hr = desktopFolder.Obj.BindToObject(parentPIDLPtr, IntPtr.Zero,
                                                                typeof(IShellFolder2).GUID, out ptrShellFolder);
                        }

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

                        // This item is not directly under the Desktop root
                        using (var shellFolder = new ShellFolder(ptrShellFolder))
                        {
                            parseName = shellFolder.GetShellFolderName(relativeChildPIDLPtr, SHGDNF.SHGDN_FORPARSING);
                            name      = shellFolder.GetShellFolderName(relativeChildPIDLPtr, SHGDNF.SHGDN_INFOLDER | SHGDNF.SHGDN_FOREDITING);
                            labelName = shellFolder.GetShellFolderName(relativeChildPIDLPtr, SHGDNF.SHGDN_NORMAL);
                        }
                    }
                }

                if (ShellHelpers.IsSpecialPath(parseName) == ShellHelpers.SpecialPath.None)
                {
                    normPath = parseName;
                }

                return(new BrowseItemFromPath(path, parseName, name, labelName,
                                              SpecialPathId, normPath
////                                               ,parentIdList, relativeChildIdList
                                              ));
            }
            finally
            {
                PidlManager.ILFree(parentPIDLPtr);
                PidlManager.ILFree(relativeChildPIDLPtr);

                if (fullPidlPtr != default(IntPtr))
                {
                    NativeMethods.ILFree(fullPidlPtr);
                }
            }
        }
Exemple #8
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);
        }