Beispiel #1
0
        /// <summary>
        /// Get PIDL and parent shellfolder for given file path
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static PIDLShellFolder GetPIDLAndParentIshellFolderForFile(string filePath)
        {
            //get desktopPtr first
            IntPtr       desktopPtr;
            IShellFolder desktop = GetDesktopFolder(out desktopPtr);

            string       fileName = Path.GetFileName(filePath);
            IShellFolder parentShellFolder;
            string       FolderPath = Directory.GetParent(filePath).FullName;
            IntPtr       Pidl = IntPtr.Zero;
            uint         i, j = 0;

            desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, FolderPath, out i, out Pidl, ref j);
            desktop.BindToObject(Pidl, IntPtr.Zero, ref Guids.IID_IShellFolder, out parentShellFolder);
            Marshal.ReleaseComObject(desktop);

            IEnumIDList fileEnum      = null;
            IEnumIDList folderEnum    = null;
            IntPtr      fileEnumPtr   = IntPtr.Zero;
            IntPtr      folderEnumPtr = IntPtr.Zero;
            IntPtr      pidlSub;
            int         celtFetched;

            if (parentShellFolder.EnumObjects(IntPtr.Zero, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out folderEnumPtr) == ShellAPI.S_OK)
            {
                folderEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(folderEnumPtr);
                while (folderEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE)
                {
                    string name = ShellAPI.GetNameByPIDL(pidlSub);
                    if (name == fileName)
                    {
                        PIDLShellFolder ps = new PIDLShellFolder {
                            PIDL = pidlSub, ShellFolder = parentShellFolder
                        };
                        Marshal.ReleaseComObject(parentShellFolder);
                        return(ps);
                    }
                }
            }

            Marshal.ReleaseComObject(parentShellFolder);
            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Get PIDL and parent shellfolder for given file path 
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static PIDLShellFolder GetPIDLAndParentIshellFolderForFile(string filePath)
        {
            //get desktopPtr first
            IntPtr desktopPtr;
            IShellFolder desktop = GetDesktopFolder(out desktopPtr);

            string fileName = Path.GetFileName(filePath);
            IShellFolder parentShellFolder;
            string FolderPath = Directory.GetParent(filePath).FullName;
            IntPtr Pidl = IntPtr.Zero;
            uint i, j = 0;
            desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, FolderPath, out i, out Pidl, ref j);
            desktop.BindToObject(Pidl, IntPtr.Zero, ref Guids.IID_IShellFolder, out parentShellFolder);
            Marshal.ReleaseComObject(desktop);

            IEnumIDList fileEnum = null;
            IEnumIDList folderEnum = null;
            IntPtr fileEnumPtr = IntPtr.Zero;
            IntPtr folderEnumPtr = IntPtr.Zero;
            IntPtr pidlSub;
            int celtFetched;

            if (parentShellFolder.EnumObjects(IntPtr.Zero, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out folderEnumPtr) == ShellAPI.S_OK)
            {
                folderEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(folderEnumPtr);
                while (folderEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE)
                {
                    string name = ShellAPI.GetNameByPIDL(pidlSub);
                    if (name == fileName)
                    {
                        PIDLShellFolder ps = new PIDLShellFolder { PIDL = pidlSub, ShellFolder = parentShellFolder };
                        Marshal.ReleaseComObject(parentShellFolder);
                        return ps;
                    }
                }
            }

            Marshal.ReleaseComObject(parentShellFolder);
            return null;
        }