/// <summary> /// Return the index of the image that has the Shell Icon for the given file/directory. /// </summary> /// <param name="path">The full path to the file/directory</param> /// <returns>The index of the image or -1 if something goes wrong.</returns> public int GetImageIndex(string path) { if (System.IO.Directory.Exists(path)) { path = System.Environment.SystemDirectory; // optimization! give all directories the same image } else if (System.IO.Path.HasExtension(path)) { path = System.IO.Path.GetExtension(path); } if (this.SmallImageCollection.ContainsKey(path)) { return(this.SmallImageCollection.IndexOfKey(path)); } try { this.SmallImageCollection.Add(path, ShellUtilities.GetFileIcon(path, true, true)); if (this.LargeImageCollection != null) { this.LargeImageCollection.Add(path, ShellUtilities.GetFileIcon(path, false, true)); } } catch (ArgumentNullException) { return(-1); } return(this.SmallImageCollection.IndexOfKey(path)); }
/// <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); } }
private void olvFiles_ItemActivate(object sender, EventArgs e) { Object rowObject = this.olvFiles.SelectedObject; if (rowObject == null) { return; } if (rowObject is DirectoryInfo) { this.textBoxFolderPath.Text = ((DirectoryInfo)rowObject).FullName; this.buttonGo.PerformClick(); } else { ShellUtilities.Execute(((FileInfo)rowObject).FullName); } }
/// <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)); } }
/// <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, "")); }
private void SetupColumns() { // Get the size of the file system entity. // Folders and errors are represented as negative numbers this.olvColumnSize.AspectGetter = delegate(object x) { if (x is DirectoryInfo) { return((long)-1); } try { return(((FileInfo)x).Length); } catch (System.IO.FileNotFoundException) { // Mono 1.2.6 throws this for hidden files return((long)-2); } }; // Show the size of files as GB, MB and KBs. By returning the actual // size in the AspectGetter, and doing the conversion in the // AspectToStringConverter, sorting on this column will work off the // actual sizes, rather than the formatted string. this.olvColumnSize.AspectToStringConverter = delegate(object x) { long sizeInBytes = (long)x; if (sizeInBytes < 0) // folder or error { return(""); } return(Coordinator.FormatFileSize(sizeInBytes)); }; this.olvColumnSize.MakeGroupies(new long[] { 0, 1024 * 1024, 512 * 1024 * 1024 }, new string[] { "Folders", "Small", "Big", "Disk space chewer" }); // Group by month-year, rather than date // This code is duplicated for FileCreated and FileModified, so we really should // create named methods rather than using anonymous delegates. this.olvColumnCreated.GroupKeyGetter = delegate(object x) { DateTime dt = ((FileSystemInfo)x).CreationTime; return(new DateTime(dt.Year, dt.Month, 1)); }; this.olvColumnCreated.GroupKeyToTitleConverter = delegate(object x) { return(((DateTime)x).ToString("MMMM yyyy")); }; // Group by month-year, rather than date this.olvColumnModified.GroupKeyGetter = delegate(object x) { DateTime dt = ((FileSystemInfo)x).LastWriteTime; return(new DateTime(dt.Year, dt.Month, 1)); }; this.olvColumnModified.GroupKeyToTitleConverter = delegate(object x) { return(((DateTime)x).ToString("MMMM yyyy")); }; // Show the system description for this object this.olvColumnFileType.AspectGetter = delegate(object x) { return(ShellUtilities.GetFileType(((FileSystemInfo)x).FullName)); }; // Show the file attributes for this object // A FlagRenderer masks off various values and draws zero or more images based // on the presence of individual bits. this.olvColumnAttributes.AspectGetter = delegate(object x) { return(((FileSystemInfo)x).Attributes); }; FlagRenderer attributesRenderer = new FlagRenderer(); attributesRenderer.ImageList = imageList1; 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.olvColumnAttributes.Renderer = attributesRenderer; // Tell the filtering subsystem that the attributes column is a collection of flags this.olvColumnAttributes.ClusteringStrategy = new FlagClusteringStrategy(typeof(FileAttributes)); }
private void SetupColumns() { // The column setup here is identical to the File Explorer example tab -- // nothing specific to the TreeListView. // The only difference is that we don't setup anything to do with grouping, // since TreeListViews can't show groups. SysImageListHelper helper = new SysImageListHelper(this.treeListView); this.olvColumnName.ImageGetter = delegate(object x) { return(helper.GetImageIndex(((MyFileSystemInfo)x).FullName)); }; // Get the size of the file system entity. // Folders and errors are represented as negative numbers this.olvColumnSize.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); } }; // Show the size of files as GB, MB and KBs. By returning the actual // size in the AspectGetter, and doing the conversion in the // AspectToStringConverter, sorting on this column will work off the // actual sizes, rather than the formatted string. this.olvColumnSize.AspectToStringConverter = delegate(object x) { long sizeInBytes = (long)x; if (sizeInBytes < 0) // folder or error { return(""); } return(Coordinator.FormatFileSize(sizeInBytes)); }; // Show the system description for this object this.olvColumnFileType.AspectGetter = delegate(object x) { return(ShellUtilities.GetFileType(((MyFileSystemInfo)x).FullName)); }; // Show the file attributes for this object // A FlagRenderer masks off various values and draws zero or images based // on the presence of individual bits. this.olvColumnAttributes.AspectGetter = delegate(object x) { return(((MyFileSystemInfo)x).Attributes); }; FlagRenderer attributesRenderer = new FlagRenderer(); attributesRenderer.ImageList = imageListSmall; 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.olvColumnAttributes.Renderer = attributesRenderer; // Tell the filtering subsystem that the attributes column is a collection of flags this.olvColumnAttributes.ClusteringStrategy = new FlagClusteringStrategy(typeof(FileAttributes)); }