Example #1
0
        public bool HaveSubFolder()
        {
            var drive = Drives.FirstOrDefault(info => info.Name == GetPath());

            if (drive != null && !drive.IsReady)
            {
                return(false);
            }
            // Get the IEnumIDList interface pointer.
            ShellAPI.IEnumIDList pEnum = null;
            try
            {
                uint hRes = ShellFolder.EnumObjects(IntPtr.Zero, ShellAPI.SHCONTF.SHCONTF_FOLDERS, out pEnum);
                if (hRes != 0)
                {
                    Marshal.ThrowExceptionForHR((int)hRes);
                }
            }

            catch (Exception ex)
            {
                // \todo
            }
            IntPtr pIDL = IntPtr.Zero;
            Int32  iGot = 0;

            // Grab the first enumeration.
            pEnum.Next(1, out pIDL, out iGot);
            return(!pIDL.Equals(IntPtr.Zero) && iGot == 1);
        }
Example #2
0
        /// <summary>
        /// Retrieves an array of ShellItem objects for sub-folders of this shell item.
        /// </summary>
        /// <returns>ArrayList of ShellItem objects.</returns>
        public ArrayList GetSubFolders()
        {
            // Make sure we have a folder.
            if (IsFolder == false)
            {
                throw new Exception("Unable to retrieve sub-folders for a non-folder.");
            }

            ArrayList arrChildren = new ArrayList();

            try
            {
                // Get the IEnumIDList interface pointer.
                ShellAPI.IEnumIDList pEnum = null;
                uint hRes = ShellFolder.EnumObjects(IntPtr.Zero, ShellAPI.SHCONTF.SHCONTF_FOLDERS, out pEnum);
                if (hRes != 0)
                {
                    Marshal.ThrowExceptionForHR((int)hRes);
                }

                IntPtr pIDL = IntPtr.Zero;
                Int32  iGot = 0;

                // Grab the first enumeration.
                pEnum.Next(1, out pIDL, out iGot);

                // Then continue with all the rest.
                while (!pIDL.Equals(IntPtr.Zero) && iGot == 1)
                {
                    // Create the new ShellItem object.
                    arrChildren.Add(new ShellItem(m_shRootShell, pIDL, this));

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

                    // Grab the next item.
                    pEnum.Next(1, out pIDL, out iGot);
                }

                // Free the interface pointer.
                if (pEnum != null)
                {
                    Marshal.ReleaseComObject(pEnum);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error:",
                                                     System.Windows.Forms.MessageBoxButtons.OK,
                                                     System.Windows.Forms.MessageBoxIcon.Error
                                                     );
            }

            return(arrChildren);
        }
Example #3
0
        /// <summary>
        /// Retrieves an array of ShellItem objects for only Test Files (.xml extensions). All others filtered out.
        /// </summary>
        /// <returns>ArrayList of ShellItem objects.</returns>
        public ArrayList GetTestFiles()
        {
            ArrayList arrChildren = new ArrayList();

            try
            {
                // Get the IEnumIDList interface pointer.
                ShellAPI.IEnumIDList pEnum = null;
                uint hRes = ShellFolder.EnumObjects(IntPtr.Zero, ShellAPI.SHCONTF.SHCONTF_NONFOLDERS, out pEnum);
                if (hRes != 0)
                {
                    Marshal.ThrowExceptionForHR((int)hRes);
                }

                IntPtr pIDL = IntPtr.Zero;
                Int32  iGot = 0;

                // Grab the first enumeration.
                pEnum.Next(1, out pIDL, out iGot);

                // Then continue with all the rest.
                while (!pIDL.Equals(IntPtr.Zero) && iGot == 1)
                {
                    // Check first is display name contains XML
                    ShellItem shItem = new ShellItem(m_shRootShell, pIDL, this);
                    if (shItem.DisplayName.Contains(".xml") == false)
                    {
                        // Create the new ShellItem object.
                        arrChildren.Add(shItem);
                    }

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

                    // Grab the next item.
                    pEnum.Next(1, out pIDL, out iGot);
                }

                // Free the interface pointer.
                if (pEnum != null)
                {
                    Marshal.ReleaseComObject(pEnum);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error:",
                                                     System.Windows.Forms.MessageBoxButtons.OK,
                                                     System.Windows.Forms.MessageBoxIcon.Error
                                                     );
            }

            return(arrChildren);
        }
Example #4
0
        /// <summary>
        /// サブアイテム配列を取得する。
        /// </summary>
        /// <returns>ShellItem のリスト。</returns>
        public List <ShellItem> GetSubItems()
        {
            //	Make sure we have a folder.
            if (this.IsFolder == false)
            {
                throw new Exception("フォルダ以外からは子アイテムは取得できません。");
            }

            List <ShellItem> arrChildren = new List <ShellItem>();

            //	IEnumIDList インターフェースポインタを取得する
            ShellAPI.IEnumIDList pEnum = null;
            uint hRes = m_ShellFolder.EnumObjects(
                IntPtr.Zero,
                ShellAPI.SHCONTF.SHCONTF_FOLDERS | ShellAPI.SHCONTF.SHCONTF_NONFOLDERS | ShellAPI.SHCONTF.SHCONTF_INCLUDEHIDDEN,
                out pEnum);

            if (hRes != 0)
            {
                Marshal.ThrowExceptionForHR((int)hRes);
            }

            //	子のIDLを列挙する
            Int32  iGot;
            IntPtr child = IntPtr.Zero;

            try
            {
                while (true)
                {
                    if (pEnum.Next(1, out child, out iGot) != 0)
                    {
                        break;
                    }
                    arrChildren.Add(new ShellItem(child, this));                     // child は作成した ShellItem に所有される
                    child = IntPtr.Zero;
                }
            }
            finally
            {
                if (child != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(child);                // 例外が起きて child の所有権が移動していなかったら自分で解放
                }
                Marshal.ReleaseComObject(pEnum);                 // pEnum は外部に公開していないので即座に Release
            }

            return(arrChildren);
        }
Example #5
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 #6
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 #7
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);
        }