/// <summary>
        /// 获取系统文件夹默认图标
        /// </summary>
        /// <param name="folderName">文件夹名称,如果想获取自定义文件夹图标,请指定完整的文件夹名称(如 F:\test)</param>
        /// <param name="isSmallIcon">是否返回小图标</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>返回一个Icon类型的文件夹图标对象</returns>
        public static Icon GetFolderIcon(string folderName, bool isSmallIcon, out long imageIndex)
        {
            imageIndex = ErrorFolderIndex;
            if (String.IsNullOrEmpty(folderName))
            {
                return(null);
            }

            SHFileInfo      shfi   = new SHFileInfo();
            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize |
                                     SHFileInfoFlags.UseFileAttributes;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }

            int iTotal = (int)SHGetFileInfo(folderName, FileAttribute.Directory, ref shfi, (uint)Marshal.SizeOf(shfi),
                                            uFlags);
            //或int iTotal = (int)SHGetFileInfo("", 0, ref shfi, (uint)Marshal.SizeOf(shfi), SHFileInfoFlags.Icon | SHFileInfoFlags.SmallIcon);
            Icon icon = null;

            if (iTotal > 0)
            {
                icon       = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
                imageIndex = shfi.iIcon.ToInt64();
            }

            DestroyIcon(shfi.hIcon); //释放资源
            return(icon);
        }
Exemple #2
0
        /// <summary>
        /// 获取磁盘驱动器图标
        /// </summary>
        /// <param name="driverMark">有效的磁盘标号,如C、D、I等等,不区分大小写</param>
        /// <param name="isSmallIcon">标识是获取小图标还是获取大图标</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>返回一个Icon类型的磁盘驱动器图标对象</returns>
        public static Icon GetDriverIcon(char driverMark, bool isSmallIcon, out long imageIndex)
        {
            imageIndex = ErrorDriverIndex;
            //非有效盘符,返回封装的磁盘图标
            if (driverMark < 'a' && driverMark > 'z' && driverMark < 'A' && driverMark > 'Z')
            {
                return(null);
            }
            string driverName = driverMark.ToString().ToUpper() + ":\\";

            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize | SHFileInfoFlags.UseFileAttributes;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }
            Icon icon = GetIcon(driverName, FileAttribute.Normal, uFlags, out imageIndex);

            if (imageIndex == ErrorFileIndex)
            {
                imageIndex = ErrorDriverIndex;
            }
            return(icon);
        }
Exemple #3
0
        /// <summary>
        /// 获取磁盘驱动器图标
        /// </summary>
        /// <param name="driverMark">有效的磁盘标号,如C、D、I等等,不区分大小写</param>
        /// <param name="isSmallIcon">标识是获取小图标还是获取大图标</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>返回一个Icon类型的磁盘驱动器图标对象</returns>
        public static Icon GetDriverIcon(char driverMark, bool isSmallIcon, out long imageIndex)
        {
            imageIndex = ErrorDriverIndex;
            //非有效盘符,返回封装的磁盘图标
            if (driverMark < 'a' && driverMark > 'z' && driverMark < 'A' && driverMark > 'Z')
            {
                return(null);
            }
            string driverName = driverMark.ToString().ToUpper() + ":\\";

            SHFileInfo      shfi   = new SHFileInfo();
            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize | SHFileInfoFlags.UseFileAttributes;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }
            int iTotal = (int)SHGetFileInfo(driverName, FileAttribute.Normal, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);
            //int iTotal = (int)SHGetFileInfo(driverName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);
            Icon icon = null;

            if (iTotal > 0)
            {
                icon       = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
                imageIndex = shfi.iIcon.ToInt64();
            }
            DestroyIcon(shfi.hIcon); //释放资源
            return(icon);
        }
Exemple #4
0
        /// <summary>
        /// 获取磁盘驱动器图标
        /// </summary>
        /// <param name="driverName">有效的磁盘标号,如C、D、I等等,不区分大小写</param>
        /// <param name="isSmallIcon">标识是获取小图标还是获取大图标</param>
        /// <returns>返回一个Icon类型的磁盘驱动器图标对象</returns>
        public static Icon GetDriverIcon(string driverName, bool isSmallIcon)
        {
            long imageIndex = ErrorDriverIndex;

            SHFileInfo      shfi   = new SHFileInfo();
            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize | SHFileInfoFlags.UseFileAttributes;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }
            int iTotal = (int)SHGetFileInfo(driverName, FileAttribute.Normal, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);
            //int iTotal = (int)SHGetFileInfo(driverName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);
            Icon icon = null;

            if (iTotal > 0)
            {
                icon       = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
                imageIndex = shfi.iIcon.ToInt64();
            }
            DestroyIcon(shfi.hIcon); //释放资源
            return(icon);
        }
        /// <summary>
        /// 获取系统文件图标
        /// </summary>
        /// <param name="fileName">文件类型的扩展名或文件的绝对路径,如果是一个exe可执行文件,请提供完整的文件名(包含路径信息)</param>
        /// <param name="isSmallIcon">是否返回小图标</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>返回一个Icon类型的文件图标对象</returns>
        public static Icon GetFileIcon(string fileName, bool isSmallIcon, out long imageIndex)
        {
            imageIndex = ErrorFileIndex;
            if (String.IsNullOrEmpty(fileName))
            {
                return(null);
            }

            SHFileInfo      shfi   = new SHFileInfo();
            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }
            FileInfo fi = new FileInfo(fileName);

            if (fi.Name.ToUpper().EndsWith(".EXE"))
            {
                uFlags |= SHFileInfoFlags.ExeType;
            }
            else
            {
                uFlags |= SHFileInfoFlags.UseFileAttributes;
            }

            int iTotal = (int)SHGetFileInfo(fileName, FileAttribute.Normal, ref shfi, (uint)Marshal.SizeOf(shfi),
                                            uFlags);
            //或int iTotal = (int)SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);
            Icon icon = null;

            if (iTotal > 0)
            {
                icon       = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
                imageIndex = shfi.iIcon.ToInt64();
            }

            DestroyIcon(shfi.hIcon); //释放资源
            return(icon);
        }
Exemple #6
0
        /// <summary>
        /// 获取系统文件图标
        /// </summary>
        /// <param name="fileName">文件类型的扩展名或文件的绝对路径,如果是一个exe可执行文件,请提供完整的文件名(包含路径信息)</param>
        /// <param name="isSmallIcon">是否返回小图标</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>返回一个Icon类型的文件图标对象</returns>
        public static Icon GetFileIcon(string fileName, bool isSmallIcon, out long imageIndex)
        {
            imageIndex = ErrorFileIndex;
            if (String.IsNullOrEmpty(fileName))
            {
                return(null);
            }
            if (fileName.IndexOf('.') < 0)
            {
                fileName = '.' + fileName;
            }
            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }
            FileInfo fi = new FileInfo(fileName);

            if (fi.Name.ToUpper().EndsWith(".EXE"))
            {
                uFlags |= SHFileInfoFlags.ExeType;
            }
            else
            {
                uFlags |= SHFileInfoFlags.UseFileAttributes;
            }
            Icon icon = GetIcon(fileName, FileAttribute.Normal, uFlags, out imageIndex);

            if (imageIndex == ErrorFileIndex)
            {
                //是否是exe文件
                if (fi.Name.ToUpper().EndsWith(".EXE"))
                {
                    imageIndex = ErrorApplicationIndex;
                }
            }
            return(icon);
        }
Exemple #7
0
 /// <summary>
 /// 获取文件类型
 /// </summary>
 /// <param name="path">文件名</param>
 /// <param name="dwAttr">文件信息</param>
 /// <param name="dwFlag">信息控制字</param>
 /// <returns></returns>
 private static string GetTypeName(string path, FileAttribute dwAttr, SHFileInfoFlags dwFlag)
 {
     try
     {
         SHFileInfo fi     = new SHFileInfo();
         int        iTotal = (int)SHGetFileInfo(path, dwAttr, ref fi, 0, dwFlag);
         if (iTotal > 0)
         {
             return(fi.szTypeName);
         }
         else
         {
             return("");
         }
     }
     catch
     {
         return("");
     }
 }
Exemple #8
0
        /// <summary>
        /// 获取系统文件夹默认图标
        /// </summary>
        /// <param name="folderName">文件夹名称,如果想获取自定义文件夹图标,请指定完整的文件夹名称(如 F:\test)</param>
        /// <param name="isSmallIcon">是否返回小图标</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>返回一个Icon类型的文件夹图标对象</returns>
        public static Icon GetFolderIcon(string folderName, bool isSmallIcon, out long imageIndex)
        {
            imageIndex = ErrorFolderIndex;
            if (String.IsNullOrEmpty(folderName))
            {
                return(null);
            }

            SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize | SHFileInfoFlags.UseFileAttributes;

            if (isSmallIcon)
            {
                uFlags |= SHFileInfoFlags.SmallIcon;
            }
            else
            {
                uFlags |= SHFileInfoFlags.LargeIcon;
            }

            return(GetIcon(folderName, FileAttribute.Directory, uFlags, out imageIndex));
        }
Exemple #9
0
        /// <summary>
        /// 获取系统图标,不要试图释放由该方法返回的图标对象,因为它指向一个公共句柄
        /// </summary>
        /// <param name="path">文件名或路径名</param>
        /// <param name="dwAttr">文件信息</param>
        /// <param name="dwFlag">信息控制字</param>
        /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>
        /// <returns>获取到的Icon对象</returns>
        private static Icon GetIcon(string path, FileAttribute dwAttr, SHFileInfoFlags dwFlag, out long imageIndex)
        {
            if (dwAttr == FileAttribute.Directory)
            {
                imageIndex = ErrorFolderIndex;
            }
            else
            {
                imageIndex = ErrorFileIndex;
            }
            SHFileInfo shfi   = new SHFileInfo();
            int        iTotal = (int)SHGetFileInfo(path, dwAttr, ref shfi, (uint)Marshal.SizeOf(shfi), dwFlag);
            //int iTotal = (int)SHGetFileInfo(path, 0, ref shfi, (uint)Marshal.SizeOf(shfi), dwFlag);
            Icon icon = null;

            if (iTotal > 0)
            {
                icon       = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
                imageIndex = shfi.iIcon.ToInt64();
            }
            DestroyIcon(shfi.hIcon); //释放资源
            return(icon);
        }
 private static extern IntPtr SHGetFileInfo(string pszPath, FileAttribute dwFileAttributes, ref SHFileInfo sfi,
                                            uint cbFileInfo, SHFileInfoFlags uFlags);
Exemple #11
0
 private static extern IntPtr SHGetFileInfo(string pszPath, FileAttribute dwFileAttributes, ref SHFileInfo sfi, uint cbFileInfo, SHFileInfoFlags uFlags);
Exemple #12
0
 /// <summary>         
 /// 获取文件类型         
 /// </summary>         
 /// <param name="path">文件名</param>         
 /// <param name="dwAttr">文件信息</param>         
 /// <param name="dwFlag">信息控制字</param>        
 /// <returns></returns>         
 private static string GetTypeName(string path, FileAttribute dwAttr, SHFileInfoFlags dwFlag)
 {
     try
     {
         SHFileInfo fi = new SHFileInfo();
         int iTotal = (int)SHGetFileInfo(path, dwAttr, ref fi, 0, dwFlag);
         if (iTotal > 0)
         {
             return fi.szTypeName;
         }
         else
         {
             return "";
         }
     }
     catch
     {
         return "";
     }
 }
Exemple #13
0
 /// <summary>         
 /// 获取系统图标,不要试图释放由该方法返回的图标对象,因为它指向一个公共句柄         
 /// </summary>         
 /// <param name="path">文件名或路径名</param>         
 /// <param name="dwAttr">文件信息</param>         
 /// <param name="dwFlag">信息控制字</param>         
 /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>         
 /// <returns>获取到的Icon对象</returns>         
 private static Icon GetIcon(string path, FileAttribute dwAttr, SHFileInfoFlags dwFlag, out long imageIndex)
 {
     if (dwAttr == FileAttribute.Directory)
     {
         imageIndex = ErrorFolderIndex;
     }
     else
     {
         imageIndex = ErrorFileIndex;
     }
     SHFileInfo shfi = new SHFileInfo();
     int iTotal = (int)SHGetFileInfo(path, dwAttr, ref shfi, (uint)Marshal.SizeOf(shfi), dwFlag);
     //int iTotal = (int)SHGetFileInfo(path, 0, ref shfi, (uint)Marshal.SizeOf(shfi), dwFlag);
     Icon icon = null;
     if (iTotal > 0)
     {
         icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
         imageIndex = shfi.iIcon.ToInt64();
     }
     DestroyIcon(shfi.hIcon); //释放资源
     return icon;
 }
 public static extern IntPtr SHGetFileInfo(
     string pszPath,
     [MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes,
     ref SHFileInfo psfi,
     int cbFileInfo,
     [MarshalAs(UnmanagedType.U4)] SHFileInfoFlags uFlags);