示例#1
0
        /// <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.AddImageToCollection(path, this.SmallImageList, ShellUtilities.GetFileIcon(path, true, true));
                this.AddImageToCollection(path, this.LargeImageList, ShellUtilities.GetFileIcon(path, false, true));
            } catch (ArgumentNullException) {
                return(-1);
            }

            return(this.SmallImageCollection.IndexOfKey(path));
        }
示例#2
0
        /// <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);
            }
        }
示例#3
0
        /// <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);
            }
        }
示例#4
0
        /// <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));
            }
        }
示例#5
0
        /// <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 &lt; 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());
        }
示例#6
0
 /// <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 &lt; 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, ""));
 }