/// <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); }
/// <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); }
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); }
/// <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>()); } }