示例#1
0
        public IEnumerable <IListItemEx> GetContents(Boolean isEnumHidden)
        {
            var folder = this.GetIShellFolder();

            if (folder == null)
            {
                yield return(null);
            }

            HResult navRes;
            var     flags = SHCONTF.FOLDERS | SHCONTF.NONFOLDERS | SHCONTF.CHECKING_FOR_CHILDREN | SHCONTF.ENABLE_ASYNC;

            if (isEnumHidden)
            {
                flags = SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.NONFOLDERS | SHCONTF.CHECKING_FOR_CHILDREN | SHCONTF.ENABLE_ASYNC;
            }

            var enumId = ShellItem.GetIEnumIDList(folder, flags, out navRes);

            this.NavigationStatus = navRes;
            uint   count;
            IntPtr pidl;

            if (enumId == null)
            {
                yield break;
            }

            var result = enumId.Next(1, out pidl, out count);
            var i      = 0;

            while (result == HResult.S_OK)
            {
                //var sb = new StringBuilder(512);
                //Shell32.SHGetPathFromIDList(pidl, sb);
                //var ext = Path.GetExtension(sb.ToString());
                var fsi = new FileSystemListItem();
                try {
                    fsi.InitializeWithParent(this.PIDL, this.ParentHandle, pidl, i++);
                } catch {
                    continue;
                }
                fsi.IsParentSearchFolder = this.IsSearchFolder;
                yield return(fsi);

                //Shell32.ILFree(pidl);
                result = enumId.Next(1, out pidl, out count);
            }

            if (result != HResult.S_FALSE)
            {
                //Marshal.ThrowExceptionForHR((int)result);
            }

            //parentItem.Dispose();
            yield break;
        }
        IListItemEx[] IListItemEx.GetSubItems(bool isEnumHidden)
        {
            TaskScheduler taskScheduler = null;

            try {
                taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            }
            catch (InvalidOperationException) {
                SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
                taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            }

            Task <FileSystemListItem[]> taskk = Task.Factory.StartNew(() => {
                var fsiList         = new List <FileSystemListItem>();
                IShellFolder folder = this.GetIShellFolder();
                HResult navRes;
                IEnumIDList enumId = ShellItem.GetIEnumIDList(folder, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS |
                                                              SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC, out navRes);
                this.NavigationStatus = navRes;
                uint count;
                IntPtr pidl;

                //if (enumId == null) {
                //  break;
                //}
                System.Windows.Forms.Application.DoEvents();
                //Thread.Sleep(1);
                //HResult result = enumId.Next(1, out pidl, out count);
                var i          = 0;
                var parentItem = this.IsSearchFolder ? this._Item : new ShellItem(this.ParsingName.ToShellParsingName());

                while (enumId.Next(1, out pidl, out count) == HResult.S_OK)
                {
                    var fsi = new FileSystemListItem();
                    fsi.InitializeWithParent(parentItem, this.ParentHandle, pidl, i++);
                    fsiList.Add(fsi);
                    Shell32.ILFree(pidl);
                    //if (this.IsSearchFolder)
                    //	Thread.Sleep(1);
                    //System.Windows.Forms.Application.DoEvents();
                    //result = enumId.Next(1, out pidl, out count);
                }

                //if (result != HResult.S_FALSE) {
                //Marshal.ThrowExceptionForHR((int)result);
                //}

                parentItem.Dispose();
                return(fsiList.ToArray());
                //yield break;
            }, CancellationToken.None, TaskCreationOptions.None, taskScheduler);

            //taskk.Start(TaskScheduler.FromCurrentSynchronizationContext());
            taskk.Wait();
            return(taskk.Result);
        }
        public IEnumerator <IListItemEx> GetEnumerator()
        {
            var folder = this.GetIShellFolder();

            if (folder == null)
            {
                yield return(null);
            }

            HResult navRes;
            var     flags  = SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS | SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT;
            var     enumId = ShellItem.GetIEnumIDList(folder, flags, out navRes);

            this.NavigationStatus = navRes;
            uint   count;
            IntPtr pidl;

            if (enumId == null)
            {
                yield break;
            }

            var result = enumId.Next(1, out pidl, out count);
            var i      = 0;

            while (result == HResult.S_OK)
            {
                var fsi = new FileSystemListItem();
                try {
                    fsi.InitializeWithParent(this.PIDL, this.ParentHandle, pidl, i++);
                } catch {
                    continue;
                }

                fsi.IsParentSearchFolder = this.IsSearchFolder;
                fsi.Dispose();
                yield return(fsi);

                result = enumId.Next(1, out pidl, out count);
            }

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

            if (result != HResult.S_FALSE)
            {
                //Marshal.ThrowExceptionForHR((int)result);
            }
            yield break;
        }
        public IEnumerator <IListItemEx> GetEnumerator()
        {
            IShellFolder folder = this.GetIShellFolder();

            if (folder == null)
            {
                yield return(null);
            }
            HResult     navRes;
            IEnumIDList enumId = ShellItem.GetIEnumIDList(folder, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS |
                                                          SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT, out navRes);

            this.NavigationStatus = navRes;
            uint   count;
            IntPtr pidl;

            if (enumId == null)
            {
                yield break;
            }

            HResult result = enumId.Next(1, out pidl, out count);
            var     i      = 0;

            //var parentItem = new ShellItem(this._Item.Pidl); //this._Item;//this.IsSearchFolder ? this._Item : new ShellItem(this.ParsingName.ToShellParsingName());
            while (result == HResult.S_OK)
            {
                var fsi = new FileSystemListItem();
                try {
                    fsi.InitializeWithParent(this._Item, this.ParentHandle, pidl, i++);
                }
                catch {
                    continue;
                }
                fsi.IsParentSearchFolder = this.IsSearchFolder;
                yield return(fsi);

                Shell32.ILFree(pidl);
                result = enumId.Next(1, out pidl, out count);
            }

            if (result != HResult.S_FALSE)
            {
                //Marshal.ThrowExceptionForHR((int)result);
            }

            //parentItem.Dispose();
            yield break;
        }
        public IEnumerable <IntPtr> GetItemsForCount(Boolean isEnumHidden)
        {
            var folder = this.GetIShellFolder();

            if (folder == null)
            {
                yield return(IntPtr.Zero);
            }

            HResult navRes;
            var     flags = SHCONTF.FOLDERS | SHCONTF.FASTITEMS | SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT;

            if (isEnumHidden)
            {
                flags = SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS | SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT;
            }
            var enumId = ShellItem.GetIEnumIDList(folder, flags, out navRes);

            this.NavigationStatus = navRes;
            uint   count;
            IntPtr pidl;

            if (enumId == null)
            {
                yield break;
            }

            var result = enumId.Next(1, out pidl, out count);

            while (result == HResult.S_OK)
            {
                yield return(pidl);

                Shell32.ILFree(pidl);
                result = enumId.Next(1, out pidl, out count);
            }

            if (folder != null)
            {
                Marshal.ReleaseComObject(folder);
            }
            if (result != HResult.S_FALSE)
            {
                //Marshal.ThrowExceptionForHR((int)result);
            }
            yield break;
        }