Example #1
0
        /// <summary>
        /// get sub shell items for path
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static DataSourceShell GetShellItem(string path)
        {
            if (!CommonProvider.Directory.Exists(path))
            {
                return(null);
            }

            try
            {
                DataSourceShell shell       = new DataSourceShell();
                var             items       = shell.GetSubItems();
                var             rootNode    = items.FirstOrDefault(o => o.ParsingName == CommonProvider.ComputerParseName);
                bool            isStop      = false;
                DataSourceShell targetShell = null;
                while (!isStop)
                {
                    var shells = rootNode.GetSubItems();

                    targetShell = shells.FirstOrDefault(o => CommonProvider.CheckIsTopLevel(o.Path, path));
                    if (targetShell.Path.IsPathEqual(path))
                    {
                        isStop = true;
                    }
                    rootNode = targetShell;
                }
                return(targetShell);
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex.Message);
                return(null);
            }
        }
 /// <summary>
 /// get  paths , from shell folder
 /// </summary>
 /// <param name="folder">shell folder</param>
 /// <returns></returns>
 public static List <string> GetShellSubPaths(DataSourceShell shell)
 {
     shellSubPaths.Clear();
     if (shell == null)
     {
         return(null);
     }
     getSubPathByRecursion(shell);
     return(shellSubPaths);
 }
        /// <summary>
        /// get path from shell
        /// </summary>
        /// <param name="shell"></param>
        static void getSubPathByRecursion(DataSourceShell shell)
        {
            var items = shell.GetSubItems();

            foreach (var item in items)
            {
                if (item.Path.IsNullOrEmpty())
                {
                    getSubPathByRecursion(item);
                }
                else
                {
                    shellSubPaths.Add(item.Path);
                }
            }
        }
Example #4
0
        public List <DataSourceShell> GetFolderByPage(int pageSize, int pageindex, out bool isEndPage)
        {
            ShellAPI.IEnumIDList shllEnum = null;
            IntPtr ptrIDLChild            = IntPtr.Zero;
            Int32  isGotChild             = 0;


            List <DataSourceShell> lst = new List <DataSourceShell>();

            LogHelper.DebugFormat("shell GetFolderByPage ==>{0}", this.ParsingName);

            if (this._blibrary) //support for windows 7 library
            {
                #region For library
                ShellAPI.IShellItem psi;
                if (ShellAPI.SHCreateShellItem(IntPtr.Zero, null, PIDL, out psi) == 0)
                {
                    Int32 length = 0;
                    if (ShellAPI.SHLoadLibraryFromItem(psi, ref length) == 0)
                    {
                        for (int i = 0; i <= length; i++)
                        {
                            ShellAPI.IShellItem psiarry;
                            if (ShellAPI.SHGetShellItemsAt(psi, i, out psiarry) == 0)
                            {
                                DataSourceShell shItem = new DataSourceShell(psiarry);
                                lst.Add(shItem);
                            }
                        }
                        isEndPage = true;
                        return(lst);
                    }
                }
                #endregion
            }


            #region init
            isEndPage = false;
            // Get the IEnumIDList interface pointer.
            ShellAPI.SHCONTF flags = ShellAPI.SHCONTF.SHCONTF_FOLDERS;
            if (shllEnum != null)
            {
                Marshal.ReleaseComObject(shllEnum);
            }

            uint result = ShellFolder.EnumObjects(IntPtr.Zero,
                                                  flags
                                                  , out shllEnum);

            if (result != 0)
            {
                isEndPage = true;
                LogHelper.Debug("result != 0   ==> true");
                return(null);
            }
            #endregion

            #region reset enum
            ptrIDLChild = IntPtr.Zero;
            isGotChild  = 0;
            shllEnum.Reset();
            #endregion

            #region skip pageIndex*pageSize
            // Grab the first enumeration.
            for (int i = 0; i <= ((pageindex - 1) * pageSize); i++)
            {
                // Free the PIDL and reset counters.
                Marshal.FreeCoTaskMem(ptrIDLChild);
                ptrIDLChild = IntPtr.Zero;
                isGotChild  = 0;

                // Grab the next item.
                shllEnum.Next(1, out ptrIDLChild, out isGotChild);
            }
            #endregion

            LogHelper.Debug("enum .....");

            #region enum item

            int itemIndex = 0;
            while (itemIndex < pageSize)
            {
                SpecialFolderType specialFolderType = CheckSpecialFolderType(shellFolder, ptrIDL, ptrIDLChild);
                if (specialFolderType != SpecialFolderType.Internet &&
                    specialFolderType != SpecialFolderType.RecycleBin &&
                    specialFolderType != SpecialFolderType.MyComputerControlPanel &&
                    specialFolderType != SpecialFolderType.DesktopControlPanel &&
                    (SpecialFolderType != SpecialFolderType.MyComputer ||
                     (specialFolderType != SpecialFolderType.MyDocuments &&
                      specialFolderType != SpecialFolderType.SharedDocuments))
                    )
                {
                    LogHelper.DebugFormat("enum-1   : {0}", ptrIDLChild);
                    // Create the new ShellItem object.
                    DataSourceShell shItem = new DataSourceShell(shellRoot, ptrIDLChild, this, false, specialFolderType);
                    if (shItem.IsFolder && !shItem.IsStream)
                    {
                        lst.Add(shItem);
                    }

                    LogHelper.DebugFormat("enum  : {0}", shItem.ParsingName);
                }
                // Free the PIDL and reset counters.
                Marshal.FreeCoTaskMem(ptrIDLChild);
                ptrIDLChild = IntPtr.Zero;
                isGotChild  = 0;

                // Grab the next item.
                shllEnum.Next(1, out ptrIDLChild, out isGotChild);
                if (ptrIDLChild.Equals(IntPtr.Zero) && isGotChild == 0)
                {
                    LogHelper.Debug("ptrIDLChild.Equals(IntPtr.Zero) && iGotChild == 0   ==> true");
                    isEndPage = true;
                    break;
                }
                itemIndex++;
            }
            #endregion

            LogHelper.Debug("enum <==");
            #region sort item
            // AH: If _getfolders flag is true, sort treeview items.
            if (this.SpecialFolderType != SpecialFolderType.MyComputer)
            {
                List <DataSourceShell>      tempLst  = new List <DataSourceShell>();
                IComparer <DataSourceShell> comparer = new SortAscending();
                for (int i = 0; isRoot && i < lst.Count; i++)
                {
                    if (lst[i].SpecialFolderType == SpecialFolderType.MyComputer ||
                        lst[i].SpecialFolderType == SpecialFolderType.DesktopControlPanel ||
                        lst[i].SpecialFolderType == SpecialFolderType.Network ||
                        lst[i].SpecialFolderType == SpecialFolderType.MyDocuments ||
                        lst[i].SpecialFolderType == SpecialFolderType.SharedDocuments ||
                        lst[i].SpecialFolderType == SpecialFolderType.CurrentUserProfile
                        )
                    {
                        tempLst.Add(lst[i]);
                        lst.RemoveAt(i);
                        i--;
                    }
                }
                lst.Sort(comparer);
                for (int i = 0; isRoot && i < tempLst.Count; i++)
                {
                    lst.Insert(i, tempLst[i]);
                }
            }
            #endregion
            LogHelper.Debug("enum end==");
            return(lst);
        }
Example #5
0
        /// <summary>
        /// Retrieves an array of ShellItem objects for sub-folders of this shell item.
        /// always sub-folders, non file
        /// </summary>
        /// <returns>ArrayList of ShellItem objects.</returns>
        public List <DataSourceShell> GetSubItems()
        {
            //this._getfolders = getfolders;

            //T#81733 - BIU support for Windows 7 libraries and the new Windows 7 open file dialog

            if (this._blibrary) //support for windows 7 library
            {
                List <DataSourceShell> lst = new List <DataSourceShell>();
                ShellAPI.IShellItem    psi;
                if (ShellAPI.SHCreateShellItem(IntPtr.Zero, null, PIDL, out psi) == 0)
                {
                    Int32 length = 0;
                    if (ShellAPI.SHLoadLibraryFromItem(psi, ref length) == 0)
                    {
                        for (int i = 0; i <= length; i++)
                        {
                            ShellAPI.IShellItem psiarry;
                            if (ShellAPI.SHGetShellItemsAt(psi, i, out psiarry) == 0)
                            {
                                DataSourceShell shItem = new DataSourceShell(psiarry);
                                lst.Add(shItem);
                            }
                        }
                        return(lst);
                    }
                }
                return(lst);
            }
            else
            {
                // Make sure we have a folder.
                if (IsFolder == false)
                {
                    throw new Exception("Unable to retrieve sub-folders for a non-folder.");
                }

                // Get the IEnumIDList interface pointer.
                ShellAPI.SHCONTF flags =
                    ShellAPI.SHCONTF.SHCONTF_INCLUDEHIDDEN;
                flags |= ShellAPI.SHCONTF.SHCONTF_FOLDERS;

                ShellAPI.IEnumIDList pEnum = null;
                if (ShellFolder != null)
                {
                    uint result = ShellFolder.EnumObjects(IntPtr.Zero,
                                                          flags
                                                          , out pEnum);
                    if (result == 0)
                    {
                        List <DataSourceShell> lst = GetPage(pEnum);
                        if (pEnum != null)
                        {
                            Marshal.ReleaseComObject(pEnum);
                        }
                        return(lst);
                    }
                }
                return(new List <DataSourceShell>());
            }
        }
Example #6
0
        /// <summary>
        /// Gets the page.
        /// </summary>
        /// <param name="getNextPage">if set to <c>true</c> [get next page].</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        List <DataSourceShell> GetPage(ShellAPI.IEnumIDList pEnum)
        {
            IntPtr pIDLChild           = IntPtr.Zero;
            Int32  iGotChild           = 0;
            List <DataSourceShell> lst = new List <DataSourceShell>();

            pEnum.Next(1, out pIDLChild, out iGotChild);
            while (!pIDLChild.Equals(IntPtr.Zero) && iGotChild == 1)
            {
                SpecialFolderType specialFolderType = CheckSpecialFolderType(shellFolder, ptrIDL, pIDLChild);
                if (specialFolderType != SpecialFolderType.Internet &&
                    specialFolderType != SpecialFolderType.RecycleBin &&
                    specialFolderType != SpecialFolderType.MyComputerControlPanel &&
                    specialFolderType != SpecialFolderType.DesktopControlPanel &&
                    (SpecialFolderType != SpecialFolderType.MyComputer ||
                     (specialFolderType != SpecialFolderType.MyDocuments &&
                      specialFolderType != SpecialFolderType.SharedDocuments))
                    )
                {
                    // Create the new ShellItem object.
                    DataSourceShell shItem = new DataSourceShell(shellRoot, pIDLChild, this, false, specialFolderType);
                    if (shItem.IsFolder && !shItem.IsStream)
                    {
                        lst.Add(shItem);
                    }
                }

                // Free the PIDL and reset counters.
                Marshal.FreeCoTaskMem(pIDLChild);
                pIDLChild = IntPtr.Zero;
                iGotChild = 0;

                // Grab the next item.
                pEnum.Next(1, out pIDLChild, out iGotChild);
            }

            if (this.SpecialFolderType != SpecialFolderType.MyComputer)
            {
                List <DataSourceShell>      tempLst  = new List <DataSourceShell>();
                IComparer <DataSourceShell> comparer = new SortAscending();
                for (int i = 0; isRoot && i < lst.Count; i++)
                {
                    if (lst[i].SpecialFolderType == SpecialFolderType.MyComputer ||
                        lst[i].SpecialFolderType == SpecialFolderType.DesktopControlPanel ||
                        lst[i].SpecialFolderType == SpecialFolderType.Network ||
                        lst[i].SpecialFolderType == SpecialFolderType.MyDocuments ||
                        lst[i].SpecialFolderType == SpecialFolderType.SharedDocuments ||
                        lst[i].SpecialFolderType == SpecialFolderType.CurrentUserProfile
                        )
                    {
                        tempLst.Add(lst[i]);
                        lst.RemoveAt(i);
                        i--;
                    }
                }
                lst.Sort(comparer);
                for (int i = 0; isRoot && i < tempLst.Count; i++)
                {
                    lst.Insert(i, tempLst[i]);
                }
            }
            return(lst);
        }
Example #7
0
        /// <summary>
        /// Constructor. Create a sub-item shell item object.
        /// </summary>
        /// <param name="shFolder">IShellFolder interface of the Desktop</param>
        /// <param name="pIDL">The fully qualified PIDL for this shell item</param>
        /// <param name="shellDesktop">The ShellItem object for desktop</param>
        public DataSourceShell(ShellAPI.IShellFolder shellDesktop, IntPtr pIDL, DataSourceShell shellParent, bool hasOnlyFolders, SpecialFolderType folderType)
        {
            _specialFolderType = folderType;

            // We need the Desktop shell item to exist first.
            if (haveRootShell == false)
            {
                throw new Exception("The root shell item must be created before creating a sub-item");
            }

            // Create the FQ PIDL for this new item.
            ptrIDL = ShellAPI.ILCombine(shellParent.PIDL, pIDL);

            shellItemPath = GetPath();

            // Get the properties of this item.
            ShellAPI.SFGAOF uFlags =
                ShellAPI.SFGAOF.SFGAO_FOLDER |
                //ShellAPI.SFGAOF.SFGAO_HASSUBFOLDER |
                ShellAPI.SFGAOF.SFGAO_BROWSABLE |
                ShellAPI.SFGAOF.SFGAO_STREAM |
                ShellAPI.SFGAOF.SFGAO_LINK |
                ShellAPI.SFGAOF.SFGAO_FILESYSTEM |
                ShellAPI.SFGAOF.SFGAO_HIDDEN |
                ShellAPI.SFGAOF.SFGAO_REMOVABLE
            ;

            // Here we get some basic attributes.
            uint result = shellParent.shellFolder.GetAttributesOf(1, ref pIDL, ref uFlags);

            isFolder     = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_FOLDER);
            hasSubFolder = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_HASSUBFOLDER);
            IsBrowsable  = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_BROWSABLE);
            IsStream     = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_STREAM);
            IsLink       = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_LINK);
            isFileSystem = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_FILESYSTEM);
            IsHidden     = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_HIDDEN);
            IsRemovable  = Convert.ToBoolean(uFlags & ShellAPI.SFGAOF.SFGAO_REMOVABLE);

            ShellAPI.IShellItem pshellitem;
            if (ShellAPI.SHCreateShellItem(IntPtr.Zero, null, PIDL, out pshellitem) == 0)
            {
                Int32 length = 0;

                if (ShellAPI.SHLoadLibraryFromItem(pshellitem, ref length) == 0)
                {
                    _blibrary = true;
                }
            }

            IntPtr parseString = IntPtr.Zero;

            pshellitem.GetDisplayName(ShellAPI.SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, out parseString);
            this.ParsingName = Marshal.PtrToStringAuto(parseString);

            LoadFileInfo(ptrIDL);
            //  [1/18/2010 jozhang #92129]
            //when IsStream and isFolder is true,it means the file is .cab or .zip so isFolder should be changed to false;
            if (IsStream && isFolder)
            {
                isFolder = false;                       // In case of zip
            }
            try
            {
                // Create the IShellFolder interface for this item.
                if (IsFolder)
                {
                    result = shellParent.shellFolder.BindToObject(pIDL, IntPtr.Zero, ref ShellAPI.IID_IShellFolder, out shellFolder);
                    if (result != 0)
                    {
                        Marshal.ThrowExceptionForHR((int)result);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Debug("shellParent.shellFolder.BindToObject Failed", ex);
            }
        }