/// <summary> /// Return the index into the system image list of the image that represents the given file. /// </summary> /// <param name="path">The full path to the file or directory whose icon is required</param> /// <returns>The index of the icon, or -1 if something goes wrong</returns> /// <remarks>This is only useful if you are using the system image lists directly. Since there is /// no way to do that in .NET, it isn't a very useful.</remarks> public static int GetSysImageIndex(string path) { SHFILEINFO shfi = new SHFILEINFO(); int flags = SHGFI_ICON | SHGFI_SYSICONINDEX; IntPtr result = ShellUtilities.SHGetFileInfo(path, 0, out shfi, Marshal.SizeOf(shfi), flags); if (result.ToInt32() == 0) { return(-1); } else { return(shfi.iIcon); } }
/// <summary> /// Get the string that describes the file's type. /// </summary> /// <param name="path">The file or directory whose type is to be fetched</param> /// <returns>A string describing the type of the file, or an empty string if something goes wrong.</returns> public static string GetFileType(string path) { SHFILEINFO shfi = new SHFILEINFO(); int flags = SHGFI_TYPENAME; IntPtr result = ShellUtilities.SHGetFileInfo(path, 0, out shfi, Marshal.SizeOf(shfi), flags); if (result.ToInt32() == 0) { return(String.Empty); } else { return(shfi.szTypeName); } }
/// <summary> /// Return the icon for the given file/directory. /// </summary> /// <param name="path">The full path to the file whose icon is to be returned</param> /// <param name="isSmallImage">True if the small (16x16) icon is required, otherwise the 32x32 icon will be returned</param> /// <param name="useFileType">If this is true, only the file extension will be considered</param> /// <returns>The icon of the given file, or null if something goes wrong</returns> public static Icon GetFileIcon(string path, bool isSmallImage, bool useFileType) { int flags = SHGFI_ICON; if (isSmallImage) { flags |= SHGFI_SMALLICON; } int fileAttributes = 0; if (useFileType) { flags |= SHGFI_USEFILEATTRIBUTES; if (System.IO.Directory.Exists(path)) { fileAttributes = FILE_ATTRIBUTE_DIRECTORY; } else { fileAttributes = FILE_ATTRIBUTE_NORMAL; } } SHFILEINFO shfi = new SHFILEINFO(); IntPtr result = ShellUtilities.SHGetFileInfo(path, fileAttributes, out shfi, Marshal.SizeOf(shfi), flags); if (result.ToInt32() == 0) { return(null); } else { return(Icon.FromHandle(shfi.hIcon)); } }
void InitializeTreeListExample() { this.treeListView.HierarchicalCheckboxes = true; this.treeListView.HideSelection = false; this.treeListView.CanExpandGetter = delegate(object x) { return(((MyFileSystemInfo)x).IsDirectory); }; this.treeListView.ChildrenGetter = delegate(object x) { MyFileSystemInfo myFileSystemInfo = (MyFileSystemInfo)x; try { return(myFileSystemInfo.GetFileSystemInfos()); } catch (UnauthorizedAccessException ex) { MessageBox.Show(this, ex.Message, "ObjectListViewDemo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(new ArrayList()); } }; // You can change the way the connection lines are drawn by changing the pen TreeListView.TreeRenderer renderer = this.treeListView.TreeColumnRenderer; renderer.LinePen = new Pen(Color.Firebrick, 0.5f); renderer.LinePen.DashStyle = DashStyle.Dot; //------------------------------------------------------------------- // Eveything after this is the same as the Explorer example tab -- // nothing specific to the TreeListView. It doesn't have the grouping // delegates, since TreeListViews can't show groups. // Draw the system icon next to the name SysImageListHelper helper = new SysImageListHelper(this.treeListView); this.colName.ImageGetter = delegate(object x) { return(helper.GetImageIndex(((MyFileSystemInfo)x).FullName)); }; // Show the size of files as GB, MB and KBs. Also, group them by // some meaningless divisions this.colSize.AspectGetter = delegate(object x) { MyFileSystemInfo myFileSystemInfo = (MyFileSystemInfo)x; if (myFileSystemInfo.IsDirectory) { return((long)-1); } try { return(myFileSystemInfo.Length); } catch (System.IO.FileNotFoundException) { // Mono 1.2.6 throws this for hidden files return((long)-2); } }; this.colSize.AspectToStringConverter = delegate(object x) { if ((long)x == -1) // folder { return(""); } return(FormatFileSize((long)x)); }; // Show the system description for this object this.colType.AspectGetter = delegate(object x) { return(ShellUtilities.GetFileType(((MyFileSystemInfo)x).FullName)); }; // Show the file attributes for this object this.colAttrs.AspectGetter = delegate(object x) { return(((MyFileSystemInfo)x).Attributes); }; FlagRenderer attributesRenderer = new FlagRenderer(); attributesRenderer.Add(FileAttributes.Archive, "archive"); attributesRenderer.Add(FileAttributes.ReadOnly, "readonly"); attributesRenderer.Add(FileAttributes.System, "system"); attributesRenderer.Add(FileAttributes.Hidden, "hidden"); attributesRenderer.Add(FileAttributes.Temporary, "temporary"); this.colAttrs.Renderer = attributesRenderer; this.colAttrs.ClusteringStrategy = new FlagClusteringStrategy(typeof(FileAttributes)); // List all drives as the roots of the tree ArrayList roots = new ArrayList(); foreach (DriveInfo di in DriveInfo.GetDrives()) { if (di.IsReady) { roots.Add(new MyFileSystemInfo(new DirectoryInfo(di.Name))); break; } } this.treeListView.Roots = roots; }
/// <summary> /// Execute the given operation on the file or directory identified by the given path. /// Example operations are "edit", "print", "explore". /// </summary> /// <param name="path">The file or directory to be operated on</param> /// <param name="operation">What operation should be performed</param> /// <returns>Values < 31 indicate some sort of error. See ShellExecute() documentation for specifics.</returns> public static int Execute(string path, string operation) { IntPtr result = ShellUtilities.ShellExecute(0, operation, path, "", "", SW_SHOWNORMAL); return(result.ToInt32()); }
/// <summary> /// Execute the default verb on the file or directory identified by the given path. /// For documents, this will open them with their normal application. For executables, /// this will cause them to run. /// </summary> /// <param name="path">The file or directory to be executed</param> /// <returns>Values < 31 indicate some sort of error. See ShellExecute() documentation for specifics.</returns> /// <remarks>The same effect can be achieved by <code>System.Diagnostics.Process.Start(path)</code>.</remarks> public static int Execute(string path) { return(ShellUtilities.Execute(path, "")); }