Exemplo n.º 1
0
        public void GetLogicalLocationForDesktopParentOf()
        {
            var testitem = KF_IID.ID_FOLDERID_ComputerFolder;

            IdList parentIdList = null, relativeChild = null;
            var    retVal = PidlManager.GetParentIdListFromPath(testitem, out parentIdList, out relativeChild);

            Assert.IsTrue(relativeChild != null);
            Assert.IsTrue(relativeChild.Size == 1);

            Assert.IsTrue(retVal == true);
            Assert.IsFalse(parentIdList == null);
            Assert.IsTrue(parentIdList.Size == 0);  // An empty list represents the desktop

            IntPtr parentPIDL = PidlManager.IdListToPidl(IdList.Create(parentIdList.Ids));

            try
            {
                Assert.IsFalse(parentPIDL == default(IntPtr));

                string path = PidlManager.GetPathFromPIDL(parentPIDL);

                Assert.IsFalse(string.IsNullOrEmpty(path));
                Assert.IsFalse(testitem.Equals(path, StringComparison.InvariantCultureIgnoreCase));

                // The expected parent of 'This PC' is the 'Desktop'
                Assert.IsTrue(path.Equals(KF_IID.ID_FOLDERID_Desktop, StringComparison.InvariantCultureIgnoreCase));

                // this works for display name return PidlManager.GetPidlDisplayName(parentPIDL);
            }
            finally
            {
                parentPIDL = PidlManager.FreeCoTaskMem(parentPIDL);
            }
        }
Exemplo n.º 2
0
        public void GetStorageLocationForSpecialFolderParent()
        {
            string originalPath = null;
            IntPtr ptrPath      = default(IntPtr);

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

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

                    originalPath = Marshal.PtrToStringUni(ptrPath);
                }

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

                string testFolder = System.IO.Path.Combine(originalPath, "System32");
                Assert.IsTrue(System.IO.Directory.Exists(testFolder));

                IdList parentIdList = null, relativeChild = null;
                var    retVal = PidlManager.GetParentIdListFromPath(testFolder, out parentIdList, out relativeChild);

                Assert.IsTrue(relativeChild != null);
                Assert.IsTrue(relativeChild.Size == 1);

                IntPtr parentPIDL = PidlManager.IdListToPidl(IdList.Create(parentIdList.Ids));
                try
                {
                    Assert.IsFalse(parentPIDL == default(IntPtr));

                    string path = PidlManager.GetPathFromPIDL(parentPIDL);

                    Assert.IsTrue(retVal == true);
                    Assert.IsFalse(parentIdList == null);

                    // Expectation: Should display a path like 'C:\' or special folder path
                    Assert.IsFalse(string.IsNullOrEmpty(testFolder));
                    Assert.IsFalse(testFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase));

                    // The expected parent of a drive is 'This PC'
                    Assert.IsTrue(path.Equals(KF_IID.ID_FOLDERID_Windows, StringComparison.InvariantCultureIgnoreCase));
                }
                finally
                {
                    parentPIDL = PidlManager.FreeCoTaskMem(parentPIDL);
                }
            }
            finally
            {
                ptrPath = PidlManager.FreeCoTaskMem(ptrPath);
            }
        }
Exemplo n.º 3
0
        public void GetLogicalLocationForParentOfDesktop()
        {
            var testitem = KF_IID.ID_FOLDERID_Desktop;

            IdList parentIdList = null, relativeChild = null;
            var    retVal = PidlManager.GetParentIdListFromPath(testitem, out parentIdList, out relativeChild);

            Assert.IsTrue(relativeChild == null);

            Assert.IsTrue(retVal == true);

            // Desktop item is expected to have no parent
            Assert.IsTrue(parentIdList == null);
        }
Exemplo n.º 4
0
        public void GetLogicalLocationForThisPCParentOf()
        {
            // Get the default drive's path
            var drive = new DirectoryInfo(Environment.SystemDirectory).Root.Name;

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

            var list = new List <string>();

            list.Add(drive);
            list.Add(KF_IID.ID_FOLDERID_Documents);
            list.Add(KF_IID.ID_FOLDERID_Music);
            list.Add(KF_IID.ID_FOLDERID_Downloads);
            list.Add(KF_IID.ID_FOLDERID_Pictures);
            list.Add(KF_IID.ID_FOLDERID_Videos);

            foreach (var testFolder in list)
            {
                IdList parentIdList = null, relativeChild = null;
                var    retVal = PidlManager.GetParentIdListFromPath(testFolder, out parentIdList, out relativeChild);

                Assert.IsTrue(relativeChild != null);
                Assert.IsTrue(relativeChild.Size == 1);

                Assert.IsTrue(retVal == true);
                Assert.IsFalse(parentIdList == null);

                IntPtr parentPIDL = PidlManager.IdListToPidl(IdList.Create(parentIdList.Ids));
                try
                {
                    Assert.IsFalse(parentPIDL == default(IntPtr));

                    string path = PidlManager.GetPathFromPIDL(parentPIDL);

                    // Expectation: Should display a path like 'C:\' or special folder path
                    Assert.IsFalse(string.IsNullOrEmpty(path));
                    Assert.IsFalse(testFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase));

                    // The expected parent of a drive is 'This PC'
                    Assert.IsTrue(path.Equals(KF_IID.ID_FOLDERID_ComputerFolder, StringComparison.InvariantCultureIgnoreCase));

                    // this works for display name return PidlManager.GetPidlDisplayName(parentPIDL);
                }
                finally
                {
                    parentPIDL = PidlManager.FreeCoTaskMem(parentPIDL);
                }
            }
        }
Exemplo n.º 5
0
        public void LoadPidls()
        {
            lock (_resolvePidlsLock)
            {
                if (_PIDLs_Initialized == true)
                {
                    return;
                }

                _PIDLs_Initialized = true;
                IdList parentIdList, relativeChildIdList;
                bool   hasPIDL = PidlManager.GetParentIdListFromPath(PathShell, out parentIdList, out relativeChildIdList);
                if (hasPIDL == true)
                {
                    _ParentIdList = parentIdList;
                    _ChildIdList  = relativeChildIdList;
                }
            }
        }
Exemplo n.º 6
0
        public void GetIconForParentOfSpecialFolder()
        {
            var testPath = KF_IID.ID_FOLDERID_Fonts;

            IdList parentIdList = null, relativeChild = null;
            var    retVal = PidlManager.GetParentIdListFromPath(testPath, out parentIdList, out relativeChild);

            Assert.IsTrue(retVal);

            // Child item is the desktop -> extract and return desktop icon
            if (parentIdList == null || relativeChild == null)
            {
                throw new NotImplementedException();
            }

            IntPtr parentPtr      = default(IntPtr);
            IntPtr relChildPtr    = default(IntPtr);
            IntPtr ptrShellFolder = default(IntPtr);
            IntPtr ptrExtractIcon = default(IntPtr);
            IntPtr smallHicon     = default(IntPtr);
            IntPtr largeHicon     = default(IntPtr);

            try
            {
                parentPtr   = PidlManager.IdListToPidl(parentIdList);
                relChildPtr = PidlManager.IdListToPidl(relativeChild);

                Assert.IsTrue(parentPtr != default(IntPtr));
                Assert.IsTrue(relChildPtr != default(IntPtr));

                Guid    guid = typeof(IShellFolder2).GUID;
                HRESULT hr   = NativeMethods.SHBindToParent(parentPtr, guid,
                                                            out ptrShellFolder, ref relChildPtr);

                Assert.IsTrue(hr == HRESULT.S_OK);

                using (var shellFolder = new ShellFolder(ptrShellFolder))
                {
                    Assert.IsTrue(shellFolder != null);

                    guid = typeof(IExtractIcon).GUID;
                    var pidls = new[] { relChildPtr };
                    hr = shellFolder.Obj.GetUIObjectOf(IntPtr.Zero, 1, pidls, guid,
                                                       IntPtr.Zero, out ptrExtractIcon);

                    Assert.IsTrue(hr == HRESULT.S_OK);

                    using (var extractIcon = new GenericCOMFolder <IExtractIcon>(ptrExtractIcon))
                    {
                        Assert.IsTrue(extractIcon != null);

                        var  iconFile = new StringBuilder(NativeMethods.MAX_PATH);
                        int  index    = -1;
                        uint pwFlags  = 0;
                        hr = extractIcon.Obj.GetIconLocation(0, iconFile, (uint)iconFile.Capacity,
                                                             ref index, ref pwFlags);

                        Assert.IsTrue(hr == HRESULT.S_OK);

                        Assert.IsFalse(string.IsNullOrEmpty(iconFile.ToString()));

                        hr = extractIcon.Obj.Extract(iconFile.ToString(), index,
                                                     ref smallHicon, ref largeHicon, 16);

                        Assert.IsTrue(hr == HRESULT.S_OK);
                        PngBitmapDecoder bitmapDecoder = null;
                        using (var memoryStream = new MemoryStream())
                        {
                            Icon.FromHandle(smallHicon).ToBitmap().Save(memoryStream, ImageFormat.Png);
                            memoryStream.Seek(0, SeekOrigin.Begin);

                            // Decode the icon
                            bitmapDecoder = new PngBitmapDecoder(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        }

                        Assert.IsTrue(bitmapDecoder != null);
                        Assert.IsTrue(bitmapDecoder.Frames != null);
                        Assert.IsTrue(bitmapDecoder.Frames.Count > 0);

                        Assert.IsTrue(bitmapDecoder.Frames[0] != null);

                        bitmapDecoder = null;
                        using (var memoryStream = new MemoryStream())
                        {
                            Icon.FromHandle(largeHicon).ToBitmap().Save(memoryStream, ImageFormat.Png);
                            memoryStream.Seek(0, SeekOrigin.Begin);

                            // Decode the icon
                            bitmapDecoder = new PngBitmapDecoder(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        }

                        Assert.IsTrue(bitmapDecoder != null);
                        Assert.IsTrue(bitmapDecoder.Frames != null);
                        Assert.IsTrue(bitmapDecoder.Frames.Count > 0);

                        Assert.IsTrue(bitmapDecoder.Frames[0] != null);
                    }
                }
            }
            finally
            {
                if (parentPtr != default(IntPtr))
                {
                    NativeMethods.ILFree(parentPtr);
                }

////                if (relChildPtr != default(IntPtr))    NOT NECESSARY
////                    NativeMethods.ILFree(relChildPtr);

                if (smallHicon != default(IntPtr))
                {
                    NativeMethods.DestroyIcon(smallHicon);
                }

                if (largeHicon != default(IntPtr))
                {
                    NativeMethods.DestroyIcon(largeHicon);
                }
            }
        }
Exemplo n.º 7
0
        public void CanGetShellFolder2()
        {
            string specialPath = KF_IID.ID_FOLDERID_UsersFiles;

            IdList parentIdList = null, relativeChild = null;
            var    retVal = PidlManager.GetParentIdListFromPath(specialPath, out parentIdList, out relativeChild);

            Assert.IsTrue(retVal);

            // Child item is the desktop -> cannot implement this for desktop since it has no parent
            if (parentIdList == null || relativeChild == null)
            {
                throw new NotImplementedException();
            }

            IntPtr parentPtr      = default(IntPtr);
            IntPtr relChildPtr    = default(IntPtr);
            IntPtr relChildPtr1   = default(IntPtr);
            IntPtr ptrShellFolder = default(IntPtr);

            try
            {
                parentPtr   = PidlManager.IdListToPidl(parentIdList);
                relChildPtr = PidlManager.IdListToPidl(relativeChild);

                Assert.IsTrue(parentPtr != default(IntPtr));
                Assert.IsTrue(relChildPtr != default(IntPtr));

                Guid    guid = typeof(IShellFolder2).GUID;
                HRESULT hr   = NativeMethods.SHBindToParent(relChildPtr, guid,
                                                            out ptrShellFolder, ref relChildPtr1);

                Assert.IsTrue(hr == HRESULT.S_OK);

                using (var shellFolder = new ShellFolder(ptrShellFolder))
                {
                    Assert.IsTrue(shellFolder != null);

                    var ParseName = shellFolder.GetShellFolderName(relChildPtr, SHGDNF.SHGDN_FORPARSING);
                    Assert.IsFalse(string.IsNullOrEmpty(ParseName));

                    var Name = shellFolder.GetShellFolderName(relChildPtr, SHGDNF.SHGDN_NORMAL);
                    Assert.IsFalse(string.IsNullOrEmpty(Name));
                }
            }
            finally
            {
                if (parentPtr != default(IntPtr))
                {
                    NativeMethods.ILFree(parentPtr);
                }

                if (relChildPtr != default(IntPtr))
                {
                    NativeMethods.ILFree(relChildPtr);
                }

                //                if (relChildPtr != default(IntPtr))
                //                    NativeMethods.ILFree(relChildPtr1);
            }
        }
Exemplo n.º 8
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);
                }
            }
        }