DirectoryInfo is a class to represent a folder in disk, it's suitable to list file system entries, but it cannot be used to represent a special folder (e.g. virtual folder that doesnt exist in the disk). you may have to use IShellFolder to enumerate these directories. DirectoryInfoEx is written to support these folders. The project is a rewrite based on Steven Roebert's C# File Browser's code and article. His code does even more than my project, his article emphasis on how to take advantage of the shell once you have a complete implementation (e.g. context menu, shell drag and drop, preview handler etc), but lack of documentation about how to create one. I rewrite part of his code (the core part) to learn how it works, and this article explain how to do the basic file operations using IShellFolder interface, in C#. Because my lack of knowledge, and the nature of DirectoryInfo / FileInfo, the new class is a lot simplier than CShellItem. Index
Obtaining PIDL Folders and Files in shell namespace can be located by PIDL (ITEMIDLIST), just like folder path (or DisplayName), there are Relative PIDL and Full PIDL, just like relative path (abc.txt) and full path (c:\abc.txt). To obtain PIDL from a string path, you can use Desktop's IShellFolder.ParseDisplayName() (see below) ShellAPI.SFGAO pdwAttributes = 0; DesktopShellFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, path, ref pchEaten, out pidlPtr, ref pdwAttributes); PIDL pidl = new PIDL(pidlPtr, false); For special directories(e.g. virtual ones like DRIVES, or special file system directories like PROFILE), CSIDL enum has a list of them, you can use SHGetSpecialFolderLocation() to obtain it's PIDL int RetVal = ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, csidl, out ptrAddr); if (ptrAddr != IntPtr.Zero) { pidl = new PIDL(ptrAddr, false); return pidl; } To obtain parent directory's PIDL, one can use PIDL.ILRemoveLastID2() : IntPtr pParent = PIDL.ILClone(pidl.Ptr); //Clone a pidl PIDL.ILRemoveLastID2(ref pParent); //Remove last item (like Path.GetDirectoryName()) PIDL pidlParent = new PIDL(pParent, false); //construct the pidl. * the original PIDL.ILRemoveLastID() is not working correctly. Desktop is the root of all shell namespace folder, you can use SHGetDesktopFolder() to obtain the IShellFolder inferface for Desktop. IntPtr ptrShellFolder = IntPtr.Zero; if (ShellAPI.SHGetDesktopFolder(out ptrShellFolder) == ShellAPI.S_OK) iShellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(ptrShellFolder, typeof(IShellFolder)); as for other directories (including non-file directory, e.g. MyComputer), you can use BindToObject(). if (Parent.ShellFolder.BindToObject(pidl.Ptr, IntPtr.Zero, ref ShellAPI.IID_IShellFolder, out ptrShellFolder) == ShellAPI.S_OK)
iShellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(ptrShellFolder, typeof(IShellFolder)); if you have the full pidl, you can use desktop's IShellFolder's BindToObject() directly : _desktopShellFolder.BindToObject(_pidlFull, IntPtr.Zero, ref guid, out ptrShellFolder); //then you can construct the shell folder directly. In Version 3, a new class named ShellFolder is added, which will dispose the pointers automatically when no longer referenced : IShellFolder _shellFolder = new ShellFolder(ptrShellFolder); //new class is added to reduce the complexity IShellFolder interface contains methods to manage the folder, e.g. :
IStorage interface contains methods for creation or manage items / subitems in the folder, e.g..
My DirectoryInfoEx implementation The implementation is simplier than CShellItem, however, as FileSystemInfoEx's PIDL is exposed (via PIDLRel and PIDL property), you can implement custom operation (e.g. Extract Icon, Context Menu) externally. IShellFolder and IStorage (generate on demand) is also exposed in DirectoryInfoEx, they are automatically destroyed when disposed, do not free them yourself. Both DirectoryInfoEx and FileInfoEx is inherited from FileSystemInfoEx, they are used as enumeration (listing) subitems, DirectoryInfoEx contains GetFiles() and GetDirectories() method for this purpose. To modify a file, use FileEx class for managing files (DirectoryEx is not implemented yet, use System.IO.Directory at this time), and FileStreamEx class for read/write files. DirectoryInfoEx (and FileInfoEx)'s constructor accept a Path or PIDL. A number of special directories is defined in DirectoryInfoEx, including DesktopDirectory, MyComputerDirectory, CurrentUserDirectory, SharedDirectory and NetworkDirectory. For other special directories, you can obtain it's PIDL by calling DirectoryInfoEx.CSIDLtoPIDL(). And a demo This demo is a simple WPF application that list the subdirectories below Desktop. The Icons are obtained using SHGetFileInfo(), which takes a full PIDL parameter. Performance improvement in version 3 I have rewrite the component which allow to construct DirectoryInfoEx without iterate from Desktop, lets say c:\temp\1, last version will iterate from Desktop -> Drives -> c:\\ -> temp -> 1, this version will construct it directly, this performance gain is not significant when browsing from desktop (should be as fast thou, because old constructor is used when browsing sub items), but it should be much faster if loading a random directory, plus less memory usage. . Beside that, I have added a cache, which is useful when refreshing directory contents. Instead of construct the subdirectory directly, it will try to reuse if created ones in cache. This feature is similar to the one used in Jim Parsells's work. PIDL subPidl = new PIDL(pidlSubItem, false); if (!listContains(_cachedDirList, subPidl)) _newDirList.Add(new DirectoryInfoEx(this, subPidl)); listRemove(_remDirList, subPidl); ... //dirList contains cache foreach (DirectoryInfoEx dir in _remDirList) listRemove(dirList, dir.PIDLRel); foreach (DirectoryInfoEx dir in _newDirList) dirList.Add(dir);
History08-22-09 version 0.1 - Initial version 08-23-09 version 0.2 - Demo updated. 11-01-09 version 0.3 - Demo no longer load Network contents, edit the converter to disable this change. - DirectoryEx (static class) added. - PIDL class is now IDisposable and free automatically now. Also added new - internal classes ShellFolder and Storage which do the same. - Performance improved, no longer construct from desktop directory. (see above) - DirectoryInfoEx and FileInfoEx is now serializable. 11-01-09 Version 0.4 - Fixed Cache not working. 11-04-09 Version 0.5 - DirectoryInfoEx/FileInfoEx works even if the path specified is not exists (Exists == false, you have to call Create() or Refresh() before using it). - Refresh(), Create(), MoveTo(), Delete(), CreateSubdirectory() Open() and related instance method added. - Constructor support Environment path (e.g. %temp%) - Test project. 11-05-09 Version 0.6 - Context menu support (ContextMenuWrapper) - Demo updated (Context menu) - FileSystemWatcherEx class added. - Fixed FileInfoEx created by EnumFiles()(which used by GetFiles() and GetFileSystemInfos()) return incorrect Parent directory. 11-08-09 Version 0.7 - Fixed Root of all FileInfoEx equals to c:\Users\{User}\Desktop instead of a GUID. - Demo updated (Context menu multiselected) 11-08-09 Version 0.8 - Fixed unable Rename item in same directory. - Fixed ContextMenuWrapper dont return OnHover message on popup. - Added QueryMenuItemsEventArgs.Command, return properly for user query items. - Demo updated (added statusbar) |

