Esempio n. 1
0
        public void Dispose()
        {
            _disposed = true;
            if (_iShellFolder2 != null)
            {
                try
                {
                    Marshal.ReleaseComObject(_iShellFolder2);
                    _iShellFolder2 = null;
                }
                catch (Exception ex) { Debug.WriteLine(ex.Message); }
                catch { Debug.WriteLine("Exception thorown when releasing IShellFolder2."); }
            }

            if (_ptrShellFolder2 != IntPtr.Zero)
            {
                try
                {
                    Marshal.Release(_ptrShellFolder2);
                }
                catch (Exception) { }
                catch { Debug.WriteLine("Exception thorown when releasing IShellFolder2's Ptr."); }
                finally
                {
                    _ptrShellFolder2 = IntPtr.Zero;
                }
            }

            GC.SuppressFinalize(this);
        }
        private static bool isVistaUp = Environment.OSVersion.Version.Major >= 6; //5 = XP, 6 = Vista

        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx

        public static bool GetIExtractImage(
            IShellFolder2 parent,
            IntPtr filePidl,
            out IntPtr iExtractImagePtr,
            out IExtractImage iExtractImage)
        {
            if (!isVistaUp)
            {
                throw new NotSupportedException("Require Vista or above.");
            }

            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    1,
                    new IntPtr[] { filePidl },
                    ref ShellAPI.IID_IExtractImage,
                    IntPtr.Zero,
                    out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return(true);
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage    = null;

                return(false);
            }
        }
Esempio n. 3
0
        protected override void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL, RefreshModeEnum mode)
        {
            base.refresh(parentShellFolder, relPIDL, fullPIDL, mode);

            if ((mode & RefreshModeEnum.FullProps) != 0 &&
                parentShellFolder != null && relPIDL != null && fullPIDL != null)
            {
                if (!FullName.StartsWith("::") && File.Exists(FullName))
                {
                    try
                    {
                        FileInfo fi = new FileInfo(FullName);
                        IsReadOnly     = fi.IsReadOnly;
                        Attributes     = fi.Attributes;
                        Length         = fi.Length;
                        LastAccessTime = fi.LastAccessTime;
                        LastWriteTime  = fi.LastWriteTime;
                        CreationTime   = fi.CreationTime;
                    }
                    catch { }
                }
                else //0.18: Uses File to return FileInfo by default
                {
                    ShellAPI.SFGAO attribute = shGetFileAttribute(fullPIDL, ShellAPI.SFGAO.READONLY);
                    IsReadOnly = (attribute & ShellAPI.SFGAO.READONLY) != 0;

                    Length = 0;
                }
            }
        }
        /// <summary>
        /// Retrieves the IContextMenu for specific items
        /// </summary>
        /// <param name="parent">the parent IShellFolder which contains the items</param>
        /// <param name="pidls">the pidls of the items for which to retrieve the IContextMenu</param>
        /// <param name="icontextMenuPtr">the pointer to the IContextMenu</param>
        /// <param name="iContextMenu">the IContextMenu for the items</param>
        /// <returns>true if the IContextMenu has been retrieved succesfully, false otherwise</returns>
        public static bool GetIContextMenu(
            IShellFolder2 parent,
            IntPtr[] pidls,
            out IntPtr iContextMenuPtr,
            out IContextMenu iContextMenu)
        {
            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    (uint)pidls.Length,
                    pidls,
                    ref ShellAPI.IID_IContextMenu,
                    IntPtr.Zero,
                    out iContextMenuPtr) == ShellAPI.S_OK)
            {
                iContextMenu =
                    (IContextMenu)Marshal.GetTypedObjectForIUnknown(
                        iContextMenuPtr, typeof(IContextMenu));

                return(true);
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu    = null;

                return(false);
            }
        }
Esempio n. 5
0
        public void Dispose()
        {
            _disposed = true;
            if (_iShellFolder2 != null)
            {
                try
                {
                    Marshal.ReleaseComObject(_iShellFolder2);
                    _iShellFolder2 = null;
                }
                catch (Exception ex) { Debug.WriteLine(ex.Message + " when releasing IShellFolder2."); }
                //catch {  }
            }

            if (_ptrShellFolder2 != IntPtr.Zero)
            {
                try
                {
                    Marshal.Release(_ptrShellFolder2);
                }
                //catch (Exception) { }
                catch { Debug.WriteLine("Exception thorown when releasing IShellFolder2's Ptr."); }
                finally
                {
                    _ptrShellFolder2 = IntPtr.Zero;
                }
            }

            GC.SuppressFinalize(this);
        }
        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx

        public static bool GetIExtractImage(
            IShellFolder2 parent,
            IntPtr filePidl,
            out IntPtr iExtractImagePtr,
            out IExtractImage iExtractImage)
        {
            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    1,
                    new IntPtr[] { filePidl },
                    ref ShellAPI.IID_IExtractImage,
                    IntPtr.Zero,
                    out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return(true);
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage    = null;

                return(false);
            }
        }
Esempio n. 7
0
        private static FileAttributes loadAttributes(IShellFolder2 iShellFolder, PIDL pidlFull, PIDL pidlRel)
        {
            FileAttributes retVal = new FileAttributes();


            //ShellAPI.SFGAO attribute = shGetFileAttribute(pidlFull, ShellAPI.SFGAO.READONLY |
            //    ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM | ShellAPI.SFGAO.FILESYSANCESTOR |
            //    ShellAPI.SFGAO.HIDDEN);
            ShellAPI.SFGAO attribute = ShellAPI.SFGAO.READONLY | ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM | ShellAPI.SFGAO.FILESYSANCESTOR;
            iShellFolder.GetAttributesOf(1, new IntPtr[] { pidlRel.Ptr }, ref attribute);

            if (!IOTools.IsZip(attribute) && (attribute & ShellAPI.SFGAO.FOLDER) != 0)
            {
                retVal |= FileAttributes.Directory;
            }
            if ((attribute & ShellAPI.SFGAO.HIDDEN) != 0)
            {
                retVal |= FileAttributes.Hidden;
            }
            if ((attribute & ShellAPI.SFGAO.READONLY) != 0)
            {
                retVal |= FileAttributes.ReadOnly;
            }

            return(retVal);
        }
Esempio n. 8
0
        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx
        public static bool GetIExtractImage(
           IShellFolder2 parent,
            IntPtr filePidl,
           out IntPtr iExtractImagePtr,
           out IExtractImage iExtractImage)
        {
            if (!isVistaUp)
                throw new NotSupportedException("Require Vista or above.");

            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        1,
                        new IntPtr[] { filePidl },
                        ref ShellAPI.IID_IExtractImage,
                        IntPtr.Zero,
                        out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return true;
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage = null;

                return false;
            }
        }
        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx
        public static bool GetIExtractImage(
           IShellFolder2 parent,
            IntPtr filePidl,
           out IntPtr iExtractImagePtr,
           out IExtractImage iExtractImage)
        {
            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        1,
                        new IntPtr[] { filePidl },
                        ref ShellAPI.IID_IExtractImage,
                        IntPtr.Zero,
                        out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return true;
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage = null;

                return false;
            }
        }
Esempio n. 10
0
        private static string loadName(IShellFolder2 iShellFolder, IntPtr ptr, ShellAPI.SHGNO uFlags)
        {
            if (iShellFolder == null)
            {
                return(null);
            }

            IntPtr ptrStr = Marshal.AllocCoTaskMem(ShellAPI.MAX_PATH * 2 + 4);

            Marshal.WriteInt32(ptrStr, 0, 0);
            StringBuilder buf = new StringBuilder(ShellAPI.MAX_PATH);

            try
            {
                if (iShellFolder.GetDisplayNameOf(ptr, uFlags, ptrStr) == ShellAPI.S_OK)
                {
                    ShellAPI.StrRetToBuf(ptrStr, ptr, buf, ShellAPI.MAX_PATH);
                }
            }
            finally
            {
                if (ptrStr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptrStr);
                }
                ptrStr = IntPtr.Zero;
            }
            return(buf.ToString());
        }
Esempio n. 11
0
        protected override void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL)
        {
            base.refresh(parentShellFolder, relPIDL, fullPIDL);

            if (parentShellFolder != null && relPIDL != null && fullPIDL != null)
            {
                ShellAPI.SFGAO attribute = shGetFileAttribute(fullPIDL, ShellAPI.SFGAO.BROWSABLE |
                                                              ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.HASSUBFOLDER);
                IsBrowsable  = (attribute & ShellAPI.SFGAO.BROWSABLE) != 0 || (attribute & ShellAPI.SFGAO.CONTENTSMASK) != 0;
                IsFileSystem = (attribute & ShellAPI.SFGAO.FILESYSTEM) != 0;
                HasSubFolder = (attribute & ShellAPI.SFGAO.HASSUBFOLDER) != 0;

                if (!FullName.StartsWith("::") && Directory.Exists(FullName))
                {
                    try
                    {
                        DirectoryInfo di = new DirectoryInfo(FullName);
                        Attributes     = di.Attributes;
                        LastAccessTime = di.LastAccessTime;
                        LastWriteTime  = di.LastWriteTime;
                        CreationTime   = di.CreationTime;
                    }
                    catch { }
                }

                initDirectoryType();
            }
        }
Esempio n. 12
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     parentDir.RequestPIDL(parentPIDL =>
     {
         init(parentShellFolder, parentPIDL, relPIDL);
     });
 }
Esempio n. 13
0
        internal DirectoryInfoEx(IShellFolder2 parentShellFolder, PIDL parentPIDL, PIDL relPIDL,
                                 bool parentIsLibrary)
        {
            init(parentShellFolder, parentPIDL, relPIDL);

            //0.22: Fix illegal PIDL for Directory under Library.ms directory
            if (parentIsLibrary)
            {
                init(FullName);
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Initialize object from <paramref name="intPtrShellFolder2"/>
        /// or <paramref name="iShellFolder2"/>. You should use only one
        /// parameter here and set the other to default if necessary.
        /// </summary>
        /// <param name="intPtrShellFolder2"></param>
        /// <param name="iShellFolder2"></param>
        protected void InitObject(IntPtr intPtrShellFolder2,
                                  IShellFolder2 iShellFolder2 = null)
        {
            _intPtrShellFolder2 = intPtrShellFolder2;

            if (_intPtrShellFolder2 != IntPtr.Zero)
            {
                Obj = (IShellFolder2)Marshal.GetTypedObjectForIUnknown(intPtrShellFolder2, typeof(IShellFolder2));
            }
            else
            {
                Obj = iShellFolder2;
            }
        }
Esempio n. 15
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     PIDL parentPIDL = parentDir.PIDL;
     try
     {
         init(parentShellFolder, parentPIDL, relPIDL);
     }
     finally
     {
         if (parentPIDL != null) parentPIDL.Free();
         parentPIDL = null;
     }
 }
Esempio n. 16
0
        /// <summary>
        /// Source: http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed == false)
            {
                if (disposing == true)
                {
                    if (Obj != null)
                    {
                        try
                        {
                            Marshal.ReleaseComObject(Obj);
                        }
                        catch
                        {
                            // Ignoring this on exit of lifetime
                        }
                        finally
                        {
                            Obj = null;
                        }
                    }

                    if (_intPtrShellFolder2 != IntPtr.Zero)
                    {
                        try
                        {
                            Marshal.Release(_intPtrShellFolder2);
                        }
                        catch { }
                        finally
                        {
                            _intPtrShellFolder2 = IntPtr.Zero;
                        }
                    }

                    GC.SuppressFinalize(this);
                }

                // There are no unmanaged resources to release, but
                // if we add them, they need to be released here.
            }

            _disposed = true;

            //// If it is available, make the call to the
            //// base class's Dispose(Boolean) method
            ////base.Dispose(disposing);
        }
Esempio n. 17
0
        /// <summary>
        /// Creates the desktop shell folder.
        /// </summary>
        /// <returns>The desktop shell folder.</returns>
        private static ShellItem CreateDesktopShellFolder()
        {
            //  Get the desktop shell folder interface.
            //IShellFolder desktopShellFolderInterface = null;
            //var result = Shell32.SHGetDesktopFolder(out desktopShellFolderInterface);
            IShellFolder2 desktopShellFolderInterface = null;
            var           result = Shell32Ext.SHGetDesktopFolder(out desktopShellFolderInterface);

            //  Validate the result.
            if (result != 0)
            {
                //  Throw the failure as an exception.
                Marshal.ThrowExceptionForHR(result);
            }

            //  Get the dekstop PDIL.
            var desktopPIDL = IntPtr.Zero;

            result = Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, CSIDL.CSIDL_DESKTOP, ref desktopPIDL);

            //  Validate the result.
            if (result != 0)
            {
                //  Throw the failure as an exception.
                Marshal.ThrowExceptionForHR(result);
            }

            //  Get the file info.
            var fileInfo = new SHFILEINFO();

            Shell32.SHGetFileInfo(desktopPIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo),
                                  SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_PIDL | SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_SYSICONINDEX);

            //  Return the Shell Folder.
            return(new ShellItem
            {
                DisplayName = fileInfo.szDisplayName,
                IconIndex = fileInfo.iIcon,
                HasSubFolders = true,
                IsFolder = true,
                ShellFolderInterface = desktopShellFolderInterface,
                PIDL = desktopPIDL,
                RelativePIDL = desktopPIDL
            });
        }
Esempio n. 18
0
        internal FileInfoEx(IShellFolder2 parentShellFolder, DirectoryInfoEx parentDir, PIDL relPIDL)
        {
            Parent = parentDir;
            PIDL parentPIDL = parentDir.PIDL;

            try
            {
                init(parentShellFolder, parentPIDL, relPIDL);
            }
            finally
            {
                if (parentPIDL != null)
                {
                    parentPIDL.Free();
                }
                parentPIDL = null;
            }
        }
 internal DirectoryInfoEx(IShellFolder2 parentShellFolder, PIDL fullPIDL)
 {
     init(parentShellFolder, fullPIDL);
 }
 internal DirectoryInfoEx(IShellFolder2 parentShellFolder, PIDL parentPIDL, PIDL relPIDL)
 {
     init(parentShellFolder, parentPIDL, relPIDL);
 }
Esempio n. 21
0
 internal static string PIDLToPath(IShellFolder2 iShellFolder, PIDL pidlRel)
 {
     return(loadName(iShellFolder, pidlRel, ShellAPI.SHGNO.FORPARSING));
 }
Esempio n. 22
0
        /// <summary>
        /// Retrieves the IContextMenu for specific items
        /// </summary>
        /// <param name="parent">the parent IShellFolder which contains the items</param>
        /// <param name="pidls">the pidls of the items for which to retrieve the IContextMenu</param>
        /// <param name="icontextMenuPtr">the pointer to the IContextMenu</param>
        /// <param name="iContextMenu">the IContextMenu for the items</param>
        /// <returns>true if the IContextMenu has been retrieved succesfully, false otherwise</returns>
        public static bool GetIContextMenu(
            IShellFolder2 parent,
            IntPtr[] pidls,
            out IntPtr iContextMenuPtr,
            out IContextMenu iContextMenu)
        {
            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        (uint)pidls.Length,
                        pidls,
                        ref ShellAPI.IID_IContextMenu,
                        IntPtr.Zero,
                        out iContextMenuPtr) == ShellAPI.S_OK)
            {
                iContextMenu =
                    (IContextMenu)Marshal.GetTypedObjectForIUnknown(
                        iContextMenuPtr, typeof(IContextMenu));

                return true;
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu = null;

                return false;
            }
        }
        protected override void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL)
        {
            base.refresh(parentShellFolder, relPIDL, fullPIDL);

            if (parentShellFolder != null && relPIDL != null && fullPIDL != null)
            {
                ShellAPI.SFGAO attribute = shGetFileAttribute(fullPIDL, ShellAPI.SFGAO.BROWSABLE |
                    ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.HASSUBFOLDER);
                IsBrowsable = (attribute & ShellAPI.SFGAO.BROWSABLE) != 0 || (attribute & ShellAPI.SFGAO.CONTENTSMASK) != 0;
                IsFileSystem = (attribute & ShellAPI.SFGAO.FILESYSTEM) != 0;
                HasSubFolder = (attribute & ShellAPI.SFGAO.HASSUBFOLDER) != 0;

                if (Directory.Exists(FullName))
                    try
                    {
                        DirectoryInfo di = new DirectoryInfo(FullName);
                        Attributes = di.Attributes;
                        LastAccessTime = di.LastAccessTime;
                        LastWriteTime = di.LastWriteTime;
                        CreationTime = di.CreationTime;
                    }
                    catch { }

                initDirectoryType();
            }
        }
Esempio n. 24
0
 private static string loadName(IShellFolder2 iShellFolder, PIDL relPidl, ShellAPI.SHGNO uFlags)
 {
     return(loadName(iShellFolder, relPidl.Ptr, uFlags));
 }
Esempio n. 25
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, PIDL fullPIDL)
 {
     init(parentShellFolder, fullPIDL);
 }
Esempio n. 26
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, PIDL fullPIDL)
 {
     init(parentShellFolder, fullPIDL);
 }
Esempio n. 27
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, PIDL parentPIDL, PIDL relPIDL)
 {
     init(parentShellFolder, parentPIDL, relPIDL);
 }
Esempio n. 28
0
        protected override void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL)
        {
            base.refresh(parentShellFolder, relPIDL, fullPIDL);

            if (parentShellFolder != null && relPIDL != null && fullPIDL != null)
            {

                if (!FullName.StartsWith("::") && File.Exists(FullName))
                    try
                    {
                        FileInfo fi = new FileInfo(FullName);
                        IsReadOnly = fi.IsReadOnly;
                        Attributes = fi.Attributes;
                        Length = fi.Length;
                        LastAccessTime = fi.LastAccessTime;
                        LastWriteTime = fi.LastWriteTime;
                        CreationTime = fi.CreationTime;
                    }
                    catch { }
                else //0.18: Uses File to return FileInfo by default
                {
                    ShellAPI.SFGAO attribute = shGetFileAttribute(fullPIDL, ShellAPI.SFGAO.READONLY);
                    IsReadOnly = (attribute & ShellAPI.SFGAO.READONLY) != 0;

                    Length = 0;
                }
            }
        }
Esempio n. 29
0
 internal ShellFolder2(IntPtr ptrShellFolder2)
 {
     _ptrShellFolder2 = ptrShellFolder2;
     _iShellFolder2 = (IShellFolder2)Marshal.GetTypedObjectForIUnknown(ptrShellFolder2, typeof(IShellFolder2));
 }
Esempio n. 30
0
 internal DirectoryInfoEx(IShellFolder2 parentShellFolder, PIDL fullPIDL)
 {
     init(parentShellFolder, fullPIDL);
 }
Esempio n. 31
0
 internal DirectoryInfoEx(IShellFolder2 parentShellFolder, PIDL parentPIDL, PIDL relPIDL)
 {
     init(parentShellFolder, parentPIDL, relPIDL);
 }
Esempio n. 32
0
 public CFolderViewImplCategoryProvider(IShellFolder2 psf) => m_psf = psf;
Esempio n. 33
0
 private static string loadName(IShellFolder2 iShellFolder, ShellAPI.SHGNO uFlags)
 {
     return(loadName(iShellFolder, new PIDL(IntPtr.Zero, false), uFlags));
 }
Esempio n. 34
0
 public static extern int SHGetDesktopFolder(out IShellFolder2 ppshf);
Esempio n. 35
0
        public void CanEnumerateDesktopFolders()
        {
            // Defines the type of items that we want to retieve below the desktop root item
            const SHCONTF flags = SHCONTF.NONFOLDERS | SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN;

            //  Get the desktop root folder.
            IntPtr        ptrDesktopFolder = default(IntPtr);
            IntPtr        enumerator       = default(IntPtr);
            IShellFolder2 iDesktopFolder   = null;

            // Enumerate over children of given shell folder item using this interface
            // https://msdn.microsoft.com/en-us/library/windows/desktop/bb761983(v=vs.85).aspx
            IEnumIDList enumIDs = null;

            try
            {
                HRESULT hr = NativeMethods.SHGetDesktopFolder(out ptrDesktopFolder);

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

                if (ptrDesktopFolder != IntPtr.Zero)
                {
                    iDesktopFolder = (IShellFolder2)Marshal.GetTypedObjectForIUnknown(ptrDesktopFolder, typeof(IShellFolder2));
                }

                Assert.IsTrue(iDesktopFolder != null);

                //  Create an enumerator and enumerate over each item.
                hr = iDesktopFolder.EnumObjects(IntPtr.Zero, flags, out enumerator);

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

                // Convert enum IntPtr to interface
                enumIDs = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(enumerator, typeof(IEnumIDList));

                Assert.IsTrue(enumIDs != null);

                uint   fetched, count = 0;
                IntPtr apidl = default(IntPtr);

                // Get one item below desktop root at a time and process by getting its display name
                for (; enumIDs.Next(1, out apidl, out fetched) == HRESULT.S_OK; count++)
                {
                    if (fetched <= 0)  // End this loop if no more items are available
                    {
                        break;
                    }

                    IntPtr ptrStr = default(IntPtr); // get strings for this item
                    try
                    {
                        string displayName = null, parseName = null;

                        ptrStr = Marshal.AllocCoTaskMem(NativeMethods.MAX_PATH * 2 + 4);
                        Marshal.WriteInt32(ptrStr, 0, 0);
                        StringBuilder buf = new StringBuilder(NativeMethods.MAX_PATH);

                        // The apidl ITEMIDLIST structures returned in the array are relative to
                        // the IShellFolder being enumerated.
                        if (iDesktopFolder.GetDisplayNameOf(apidl, SHGDNF.SHGDN_NORMAL, ptrStr) == HRESULT.S_OK)
                        {
                            NativeMethods.StrRetToBuf(ptrStr, ptrDesktopFolder, buf, NativeMethods.MAX_PATH);
                            displayName = buf.ToString();
                        }

                        if (iDesktopFolder.GetDisplayNameOf(apidl, SHGDNF.SHGDN_FORPARSING, ptrStr) == HRESULT.S_OK)
                        {
                            NativeMethods.StrRetToBuf(ptrStr, ptrDesktopFolder, buf, NativeMethods.MAX_PATH);
                            parseName = buf.ToString();
                        }

                        Assert.IsFalse(string.IsNullOrEmpty(displayName));
                        Assert.IsFalse(string.IsNullOrEmpty(parseName));
                    }
                    finally
                    {
                        ptrStr = PidlManager.FreeCoTaskMem(ptrStr);
                        apidl  = PidlManager.FreeCoTaskMem(apidl);
                    }
                }

                // There should be more than one item below the desktop root item
                // 'My PC', 'Recycle Bin', and 'Network' are already three
                Assert.IsTrue(count > 2);
            }
            finally
            {
                if (enumerator != default(IntPtr))
                {
                    Marshal.Release(enumerator);
                }

                if (iDesktopFolder != null)
                {
                    Marshal.ReleaseComObject(iDesktopFolder);
                }

                if (ptrDesktopFolder != default(IntPtr))
                {
                    Marshal.Release(ptrDesktopFolder);
                }
            }
        }
Esempio n. 36
0
 /// <summary>
 /// Class constructor from <see cref="IShellFolder2"/> interface.
 /// </summary>
 /// <param name="iShellFolder2"></param>
 public ShellFolder(IShellFolder2 iShellFolder2)
     : this()
 {
     InitObject(IntPtr.Zero, iShellFolder2);
 }
Esempio n. 37
0
        protected virtual void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL, RefreshModeEnum mode)
        {
            if (parentShellFolder != null && fullPIDL != null && relPIDL != null)
            {
                Attributes = loadAttributes(parentShellFolder, fullPIDL, relPIDL);
                string parseName = loadName(parentShellFolder, relPIDL, ShellAPI.SHGNO.FORPARSING);
                FullName = "";

                //Console.WriteLine("relPIDL.size = {0}", relPIDL.Size);
                //Console.WriteLine("PIDL.size = {0}", _pidl.Size);


                if (relPIDL.Size == 0)
                {
                    FullName = IOTools.IID_Desktop;
                }
                else
                //0.12: Fixed Fullname of User/Shared directory under desktop is now it's GUID instead of it's file path.
                //0.13: Fixed All subdirectory under User/Shared directory uses GUID now.
                {
                    if (DirectoryInfoEx.CurrentUserDirectory != null)
                    {
                        if (parseName == DirectoryInfoEx.CurrentUserDirectory.FullName &&
                            loadName(parentShellFolder, ShellAPI.SHGNO.FORPARSING) == Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
                        {
                            FullName = IOTools.IID_UserFiles;
                        }
                        //else if (
                        //    (parseName.StartsWith(DirectoryInfoEx.CurrentUserDirectory.FullName) &&
                        //    _parent != null && _parent.FullName.StartsWith(IOTools.IID_UserFiles))
                        //    ||
                        //    (OriginalPath != null && OriginalPath.StartsWith(IOTools.IID_UserFiles))
                        //    )
                        //{
                        //    FullName = parseName.Replace(DirectoryInfoEx.CurrentUserDirectory.FullName, IOTools.IID_UserFiles);
                        //}
                    }

                    if (DirectoryInfoEx.SharedDirectory != null)
                    {
                        if (parseName == DirectoryInfoEx.SharedDirectory.FullName &&
                            loadName(parentShellFolder, ShellAPI.SHGNO.FORPARSING) == Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
                        {
                            FullName = IOTools.IID_Public;
                        }
                        //else if (
                        //    (parseName.StartsWith(DirectoryInfoEx.SharedDirectory.FullName) &&
                        //    _parent != null && _parent.FullName.StartsWith(IOTools.IID_Public))
                        //    ||
                        //    (OriginalPath != null && OriginalPath.StartsWith(IOTools.IID_Public))
                        //    )
                        //    FullName = parseName.Replace(DirectoryInfoEx.SharedDirectory.FullName, IOTools.IID_Public);
                    }

                    //if (_parent != null && _parent.FullName.StartsWith(IOTools.IID_Library)
                    //    && !parseName.StartsWith(IOTools.IID_Library))
                    //    FullName = PathEx.Combine(_parent.FullName, PathEx.GetFileName(parseName));

                    if (FullName == "")
                    {
                        FullName = parseName;
                    }
                }
                //if (DirectoryInfoEx.CurrentUserDirectory != null && parseName == DirectoryInfoEx.CurrentUserDirectory.FullName &&
                //loadName(parentShellFolder, ShellAPI.SHGNO.FORPARSING) == Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
                //    FullName = IOTools.IID_UserFiles;
                //else

                //if (DirectoryInfoEx.SharedDirectory != null && parseName == DirectoryInfoEx.SharedDirectory.FullName &&
                //    loadName(parentShellFolder, ShellAPI.SHGNO.FORPARSING) == Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
                //    FullName = IOTools.IID_Public;
                //else


                if (OriginalPath == null)
                {
                    OriginalPath = FullName;
                }
                if (parseName.StartsWith("::")) //Guid
                {
                    parseName = loadName(parentShellFolder, relPIDL, ShellAPI.SHGNO.NORMAL);
                }

                _name = FullName.EndsWith("\\") ? FullName : PathEx.GetFileName(FullName);
                Label = loadName(parentShellFolder, relPIDL, ShellAPI.SHGNO.NORMAL);
            }
            else
            {
                if (OriginalPath != null)
                {
                    string origPath = Helper.RemoveSlash(OriginalPath);
                    _name    = Path.GetFileName(origPath);
                    Label    = _name;
                    FullName = origPath;
                }
            }
        }
Esempio n. 38
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, PIDL parentPIDL, PIDL relPIDL)
 {
     init(parentShellFolder, parentPIDL, relPIDL);
 }
Esempio n. 39
0
 internal ShellFolder2(IntPtr ptrShellFolder2)
 {
     _ptrShellFolder2 = ptrShellFolder2;
     _iShellFolder2   = (IShellFolder2)Marshal.GetTypedObjectForIUnknown(ptrShellFolder2, typeof(IShellFolder2));
 }
Esempio n. 40
0
        internal DirectoryInfoEx(IShellFolder2 parentShellFolder, PIDL parentPIDL, PIDL relPIDL,
            bool parentIsLibrary)
        {
            init(parentShellFolder, parentPIDL, relPIDL);

            //0.22: Fix illegal PIDL for Directory under Library.ms directory
            if (parentIsLibrary)
            {
                init(FullName);
            }
        }