C#获取系统文件、文件夹和磁盘驱动器图标

本文介绍三种获取系统文件图标的方法:通过API调用、使用System.Drawing.Icon类和注册表调用API方式。此外还介绍了如何获取文件类型描述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开发一款软件需要用到获取系统文件的图标,在网上找了一下发现有以下方式获取:

1、调用API的方式获取

[csharp]  view plain  copy
  1. public class SystemIcon  
  2. {  
  3.     /// <summary>           
  4.     /// 引用shell32文件的SHGetFileInfo API方法           
  5.      /// </summary>           
  6.     /// <param name="pszPath">指定的文件名,如果为""则返回文件夹的</param>           
  7.     /// <param name="dwFileAttributes">文件属性</param>           
  8.     /// <param name="sfi">返回获得的文件信息,是一个记录类型</param>           
  9.     /// <param name="cbFileInfo">文件的类型名</param>          
  10.     /// <param name="uFlags">文件信息标识</param>           
  11.     /// <returns>-1失败</returns>           
  12.     [DllImport("shell32", EntryPoint = "SHGetFileInfo", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]  
  13.     private static extern IntPtr SHGetFileInfo(string pszPath, FileAttribute dwFileAttributes, ref SHFileInfo sfi, uint cbFileInfo, SHFileInfoFlags uFlags);  
  14.   
  15.     /// <summary>     
  16.     /// 返回系统设置的图标     
  17.      /// </summary>     
  18.     /// <param name="lpszFile">文件名,指定从exe文件或dll文件引入icon</param>     
  19.     /// <param name="nIconIndex">文件的图表中的第几个,指定icon的索引如果为0则从指定的文件中引入第1个icon</param>     
  20.     /// <param name="phiconLarge">返回的大图标的指针,大图标句柄如果为null则为没有大图标</param>     
  21.     /// <param name="phiconSmall">返回的小图标的指针,小图标句柄如果为null则为没有小图标</param>     
  22.     /// <param name="nIcons">ico个数,找几个图标</param>     
  23.     /// <returns></returns  
  24.     [DllImport("shell32.dll")]  
  25.     public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);  
  26.   
  27.     [DllImport("User32.dll", EntryPoint = "DestroyIcon")]  
  28.     public static extern int DestroyIcon(IntPtr hIcon);  
  29.   
  30.     /// <summary>           
  31.     /// 文件信息标识枚举类,所有枚举定义值前省略SHGFI投标,比如Icon 完整名称应为SHGFI_ICON           
  32.     /// </summary>           
  33.     [Flags]  
  34.     private enum SHFileInfoFlags : uint  
  35.     {  
  36.         /// <summary>               
  37.         /// 允许有叠加图案的文件图标,该标识必须和Icon同时使用               
  38.          /// </summary>               
  39.         AddOveylays = 0x20,         // SHGFI_AddOverlays = 0x000000020  
  40.         /// <summary>               
  41.         /// 只获取由参数FileAttribute指定的文件信息,并将其写入SHFileInfo结构的dwAttributes属性,如果不指定该标识,将同时获取所有文件信息。该标志不能和Icon标识同时使用               
  42.           /// </summary>               
  43.         Attr_Specified = 0x20000,   //  SHGFI_SpecifiedAttributes = 0x000020000  
  44.         /// <summary>               
  45.         /// 将获取的文件属性复制到SHFileInfo结构的dwAttributes属性中               
  46.           /// </summary>               
  47.         Attributes = 0x800,     // SHGFI_Attributes = 0x000000800  
  48.         /// <summary>               
  49.         /// 获取文件的显示名称(长文件名称),将其复制到SHFileInfo结构的dwAttributes属性中               
  50.           /// </summary>               
  51.         DisplayName = 0x200,    // SHGFI_DisplayName = 0x000000200  
  52.         /// <summary>              
  53.         /// 如果文件是可执行文件,将检索其信息,并将信息作为返回值返回                
  54.          /// </summary>               
  55.         ExeType = 0x2000,       // SHGFI_EXEType = 0x000002000  
  56.         /// <summary>               
  57.         /// 获得图标和索引,将图标句柄返回到SHFileInfo结构的hIcon属性中,索引返回到iIcon属性中               
  58.          /// </summary>               
  59.         Icon = 0x100,           // SHGFI_Icon = 0x000000100  
  60.         /// <summary>               
  61.         /// 检索包含图标的文件,并将文件名,图标句柄,图标索引号,放回到SHFileInfo结构中               
  62.         /// </summary>               
  63.         IconLocation = 0x1000,  // SHGFI_IconLocation = 0x000001000  
  64.         /// <summary>               
  65.         /// 获得大图标,该标识必须和Icon标识同时使用               
  66.          /// </summary>               
  67.         LargeIcon = 0x0,        // SHGFI_LargeIcon = 0x000000000  
  68.         /// <summary>               
  69.         /// 获取链接覆盖文件图标,该标识必须和Icon标识同时使用。               
  70.          /// </summary>               
  71.         LinkOverlay = 0x8000,   // SHGFI_LinkOverlay = 0x000008000  
  72.         /// <summary>               
  73.         /// 获取文件打开时的图标,该标识必须和Icon或SysIconIndex同时使用               
  74.          /// </summary>               
  75.         OpenIcon = 0x2,         //  SHGFI_OpenIcon = 0x000000002  
  76.         /// <summary>               
  77.         /// 获取链接覆盖文件图标索引,该标识必须和Icon标识同时使用。               
  78.          /// </summary>               
  79.         OverlayIndex = 0x40,    // SHGFI_OverlayIndex = 0x000000040  
  80.         /// <summary>               
  81.         /// 指示传入的路径是一个ITEMIDLIST结构的文件地址而不是一个路径名。               
  82.          /// </summary>               
  83.         Pidl = 0x8,             // SHGFI_PIDL = 0x000000008  
  84.         /// <summary>               
  85.         /// 获取系统的高亮显示图标,该标识必须和Icon标识同时使用。               
  86.          /// </summary>               
  87.         Selected = 0x10000,     // SHGFI_SelectedState = 0x000010000  
  88.         /// <summary>               
  89.         /// 获取 Shell-sized icon ,该标志必须和Icon标识同时使用。               
  90.          /// </summary>               
  91.         ShellIconSize = 0x4,    // SHGFI_ShellIconSize = 0x000000004  
  92.         /// <summary>               
  93.         /// 获得小图标,该标识必须和Icon或SysIconIndex同时使用。               
  94.          /// </summary>               
  95.         SmallIcon = 0x1,       // SHGFI_SmallIcon = 0x000000001  
  96.         /// <summary>               
  97.         /// 获取系统图像列表图标索引,返回系统图像列表句柄               
  98.          /// </summary>               
  99.         SysIconIndex = 0x4000,  // SHGFI_SysIconIndex = 0x000004000  
  100.         /// <summary>               
  101.         /// 获得文件类型,类型字符串被写入SHFileInfo结构的szTypeName属性中               
  102.          /// </summary>               
  103.         TypeName = 0x400,       // SHGFI_TypeName = 0x000000400  
  104.         /// <summary>               
  105.         /// 指示如果由pszPath指定的路径不存在,SHGetFileInfo方法变不会试图去操作文件。指示返回与文件类型相关的信息。该标识不能和Attributes、ExeType和Pidl同时使用               
  106.          /// </summary>               
  107.         UseFileAttributes = 0x10    // SHGFI_UserFileAttributes = 0x000000010,  
  108.     }  
  109.   
  110.     /// <summary>           
  111.     /// 文件属性枚举           
  112.      /// </summary>           
  113.     [Flags]  
  114.     private enum FileAttribute  
  115.     {  
  116.         ReadOnly = 0x00000001,  
  117.         Hidden = 0x00000002,  
  118.         System = 0x00000004,  
  119.         Directory = 0x00000010,     //路径信息               
  120.         Archive = 0x00000020,  
  121.         Device = 0x00000040,  
  122.         Normal = 0x00000080,        //普通文件信息               
  123.         Temporary = 0x00000100,  
  124.         Sparse_File = 0x00000200,  
  125.         Reparse_Point = 0x00000400,  
  126.         Compressed = 0x00000800,  
  127.         Offline = 0x00001000,  
  128.         Not_Content_Indexed = 0x00002000,  
  129.         Encrypted = 0x00004000  
  130.     }  
  131.   
  132.     /// <summary>          
  133.     /// 定义返回的文件信息结构           
  134.      /// </summary>           
  135.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  136.     private struct SHFileInfo  
  137.     {  
  138.         /// <summary>               
  139.         /// 文件的图标句柄               
  140.          /// </summary>               
  141.         public IntPtr hIcon;  
  142.         /// <summary>               
  143.         /// 图标的系统索引号               
  144.          /// </summary>               
  145.         public IntPtr iIcon;  
  146.         /// <summary>               
  147.         /// 文件的属性值,由FileAttribute指定的属性。               
  148.          /// </summary>               
  149.         public uint dwAttributes;  
  150.         /// <summary>              
  151.         /// 文件的显示名,如果是64位系统,您可能需要制定SizeConst=258而非260才能够显示完整的 TypeName               
  152.         /// </summary>               
  153.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]  
  154.         public string szDisplayName;  
  155.         /// <summary>               
  156.         /// 文件的类型名               
  157.          /// </summary>               
  158.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]  
  159.         public string szTypeName;  
  160.     }  
  161.   
  162.     /// <summary>           
  163.     /// 当读取文件图标失败的默认图标索引号           
  164.      /// </summary>           
  165.     public static readonly long ErrorFileIndex = -2;  
  166.     /// <summary>           
  167.     /// 当读取文件夹图标失败的默认图标索引号           
  168.      /// </summary>           
  169.     public static readonly long ErrorFolderIndex = -4;  
  170.     /// <summary>           
  171.     /// 当读取磁盘驱动器图标失败的默认图标索引号           
  172.      /// </summary>           
  173.     public static readonly long ErrorDriverIndex = -8;  
  174.     /// <summary>           
  175.     /// 当读取可执行文件图标失败的默认图标索引号           
  176.      /// </summary>           
  177.     public static readonly long ErrorApplicationIndex = -16;  
  178.   
  179.     /// <summary>  
  180.     /// 获取文件类型的关联图标  
  181.      /// </summary>  
  182.     /// <param name="fileName">文件类型的扩展名或文件的绝对路径</param>  
  183.     /// <param name="isSmallIcon">是否返回小图标</param>  
  184.     /// <returns>返回一个Icon类型的文件图标对象</returns>  
  185.     public static Icon GetFileIcon(string fileName, bool isSmallIcon)  
  186.     {  
  187.         long imageIndex;  
  188.         return GetFileIcon(fileName, isSmallIcon, out imageIndex);  
  189.     }  
  190.   
  191.     /// <summary>  
  192.     /// 获取系统文件图标  
  193.      /// </summary>  
  194.     /// <param name="fileName">文件类型的扩展名或文件的绝对路径,如果是一个exe可执行文件,请提供完整的文件名(包含路径信息)</param>  
  195.     /// <param name="isSmallIcon">是否返回小图标</param>  
  196.     /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>  
  197.     /// <returns>返回一个Icon类型的文件图标对象</returns>  
  198.     public static Icon GetFileIcon(string fileName, bool isSmallIcon,out long imageIndex)  
  199.     {  
  200.         imageIndex = ErrorFileIndex;  
  201.         if (String.IsNullOrEmpty(fileName))  
  202.             return null;  
  203.   
  204.         SHFileInfo shfi = new SHFileInfo();  
  205.         SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize;  
  206.         if (isSmallIcon)  
  207.             uFlags |= SHFileInfoFlags.SmallIcon;  
  208.         else  
  209.             uFlags |= SHFileInfoFlags.LargeIcon;  
  210.         FileInfo fi = new FileInfo(fileName);  
  211.         if (fi.Name.ToUpper().EndsWith(".EXE"))  
  212.             uFlags |= SHFileInfoFlags.ExeType;  
  213.         else  
  214.             uFlags |= SHFileInfoFlags.UseFileAttributes;  
  215.   
  216.         int iTotal = (int)SHGetFileInfo(fileName, FileAttribute.Normal, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);  
  217.         //或int iTotal = (int)SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);  
  218.         Icon icon = null;  
  219.         if (iTotal > 0)  
  220.         {  
  221.             icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;  
  222.             imageIndex = shfi.iIcon.ToInt64();  
  223.         }  
  224.         DestroyIcon(shfi.hIcon); //释放资源  
  225.         return icon;  
  226.     }  
  227.   
  228.     /// <summary>    
  229.     /// 获取系统文件夹默认图标  
  230.      /// </summary>    
  231.     /// <param name="isSmallIcon">是否返回小图标</param>  
  232.     /// <returns>返回一个Icon类型的文件夹图标对象</returns>  
  233.     public static Icon GetFolderIcon(bool isSmallIcon)  
  234.     {  
  235.         long imageIndex;  
  236.         return GetFolderIcon(isSmallIcon, out imageIndex);  
  237.     }  
  238.   
  239.     /// <summary>    
  240.     /// 获取系统文件夹默认图标  
  241.      /// </summary>    
  242.     /// <param name="isSmallIcon">是否返回小图标</param>  
  243.     /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>  
  244.     /// <returns>返回一个Icon类型的文件夹图标对象</returns>  
  245.     public static Icon GetFolderIcon(bool isSmallIcon,out long imageIndex)  
  246.     {  
  247.         return GetFolderIcon(Environment.SystemDirectory, isSmallIcon, out imageIndex);  
  248.     }  
  249.   
  250.     /// <summary>    
  251.     /// 获取系统文件夹默认图标  
  252.      /// </summary>    
  253.     /// <param name="folderName">文件夹名称,如果想获取自定义文件夹图标,请指定完整的文件夹名称(如 F:\test)</param>  
  254.     /// <param name="isSmallIcon">是否返回小图标</param>  
  255.     /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>  
  256.     /// <returns>返回一个Icon类型的文件夹图标对象</returns>  
  257.     public static Icon GetFolderIcon(string folderName,bool isSmallIcon, out long imageIndex)  
  258.     {  
  259.         imageIndex = ErrorFolderIndex;  
  260.         if (String.IsNullOrEmpty(folderName))  
  261.             return null;  
  262.   
  263.         SHFileInfo shfi = new SHFileInfo();  
  264.         SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize | SHFileInfoFlags.UseFileAttributes;  
  265.         if (isSmallIcon)  
  266.             uFlags |= SHFileInfoFlags.SmallIcon;  
  267.         else  
  268.             uFlags |= SHFileInfoFlags.LargeIcon;  
  269.   
  270.         int iTotal = (int)SHGetFileInfo(folderName, FileAttribute.Directory, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);  
  271.         //或int iTotal = (int)SHGetFileInfo("", 0, ref shfi, (uint)Marshal.SizeOf(shfi), SHFileInfoFlags.Icon | SHFileInfoFlags.SmallIcon);  
  272.         Icon icon = null;  
  273.         if (iTotal > 0)  
  274.         {  
  275.             icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;  
  276.             imageIndex = shfi.iIcon.ToInt64();  
  277.         }  
  278.         DestroyIcon(shfi.hIcon); //释放资源  
  279.         return icon;  
  280.     }  
  281.   
  282.     /// <summary>           
  283.     /// 获取磁盘驱动器图标  
  284.      /// </summary>           
  285.     /// <param name="driverMark">有效的磁盘标号,如C、D、I等等,不区分大小写</param>           
  286.     /// <param name="isSmallIcon">标识是获取小图标还是获取大图标</param>           
  287.     /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>           
  288.     /// <returns>返回一个Icon类型的磁盘驱动器图标对象</returns>           
  289.     public static Icon GetDriverIcon(char driverMark, bool isSmallIcon)  
  290.     {  
  291.         long imageIndex;  
  292.         return GetDriverIcon(driverMark,isSmallIcon,out imageIndex);  
  293.     }  
  294.   
  295.     /// <summary>           
  296.     /// 获取磁盘驱动器图标  
  297.      /// </summary>           
  298.     /// <param name="driverMark">有效的磁盘标号,如C、D、I等等,不区分大小写</param>           
  299.     /// <param name="isSmallIcon">标识是获取小图标还是获取大图标</param>           
  300.     /// <param name="imageIndex">输出与返回图标对应的系统图标索引号</param>           
  301.     /// <returns>返回一个Icon类型的磁盘驱动器图标对象</returns>           
  302.     public static Icon GetDriverIcon(char driverMark, bool isSmallIcon, out long imageIndex)  
  303.     {  
  304.         imageIndex = ErrorDriverIndex;  
  305.         //非有效盘符,返回封装的磁盘图标               
  306.         if (driverMark < 'a' && driverMark > 'z' && driverMark < 'A' && driverMark > 'Z')  
  307.         {  
  308.             return null;  
  309.         }  
  310.         string driverName = driverMark.ToString().ToUpper() + ":\\";  
  311.   
  312.         SHFileInfo shfi = new SHFileInfo();  
  313.         SHFileInfoFlags uFlags = SHFileInfoFlags.Icon | SHFileInfoFlags.ShellIconSize | SHFileInfoFlags.UseFileAttributes;  
  314.         if (isSmallIcon)  
  315.             uFlags |= SHFileInfoFlags.SmallIcon;  
  316.         else  
  317.             uFlags |= SHFileInfoFlags.LargeIcon;  
  318.         int iTotal = (int)SHGetFileInfo(driverName, FileAttribute.Normal, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);  
  319.         //int iTotal = (int)SHGetFileInfo(driverName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags);  
  320.         Icon icon = null;  
  321.         if (iTotal > 0)  
  322.         {  
  323.             icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;  
  324.             imageIndex = shfi.iIcon.ToInt64();  
  325.         }  
  326.         DestroyIcon(shfi.hIcon); //释放资源  
  327.         return icon;  
  328.     }  
  329. }  

2、调用System.Drawing.Icon类的ExtractAssociatedIcon(string filePath)方法:返回指定文件中包含的图像的图标表示形式

      这种方式其实也是封装了对系统的API调用,跟第一种方式差不多,不过这种方式只能获取到文件的图标,不能够获取到文件夹的图标,这个有点遗憾!

     

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 获取文件图标  
  3. /// </summary>  
  4. /// <param name="filePath">文件路径以及名称(文件全路径)</param>  
  5. /// <returns>返回一个Icon类型的文件图标对象</returns>  
  6. public static Icon GetFileExtractAssociatedIcon(string filePath)  
  7. {  
  8.      Icon icon = Icon.ExtractAssociatedIcon(filePath);  
  9.      return icon;  
  10. }  

3、使用注册表调用API方式

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 给出文件扩展名(.*),返回相应图标;若不以"."开头则返回文件夹的图标。  
  3.  /// </summary>  
  4. /// <param name="fileType">文件扩展名</param>  
  5. /// <param name="isLarge">是否返回大图标</param>  
  6. /// <returns>返回一个Icon类型的图标对象</returns>  
  7. public static Icon GetIconByFileType(string fileType, bool isLarge)  
  8. {  
  9.     if (fileType == null || fileType.Equals(string.Empty))  
  10.         return null;  
  11.     RegistryKey regVersion = null;  
  12.     string regFileType = null;  
  13.     string regIconString = null;  
  14.     string systemDirectory = Environment.SystemDirectory + "\\";  
  15.     if (fileType[0] == '.')  
  16.     {  
  17.         //读系统注册表中文件类型信息  
  18.         regVersion = Registry.ClassesRoot.OpenSubKey(fileType, true);  
  19.         if (regVersion != null)  
  20.         {  
  21.             regFileType = regVersion.GetValue(""as string;  
  22.             regVersion.Close();  
  23.             regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon"true);  
  24.             if (regVersion != null)  
  25.             {  
  26.                 regIconString = regVersion.GetValue(""as string;  
  27.                 regVersion.Close();  
  28.             }  
  29.         }  
  30.         if (regIconString == null)  
  31.         {  
  32.             //没有读取到文件类型注册信息,指定为未知文件类型的图标  
  33.             regIconString = systemDirectory + "shell32.dll,0";  
  34.         }  
  35.     }  
  36.     else  
  37.     {  
  38.         //直接指定为文件夹图标  
  39.         regIconString = systemDirectory + "shell32.dll,3";  
  40.     }  
  41.     string[] fileIcon = regIconString.Split(new char[] { ',' });  
  42.     if (fileIcon.Length != 2)  
  43.     {  
  44.         //系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标  
  45.         fileIcon = new string[] { systemDirectory + "shell32.dll""2" };  
  46.     }  
  47.     Icon resultIcon = null;  
  48.     try  
  49.     {  
  50.         //调用API方法读取图标  
  51.         int[] phiconLarge = new int[1];  
  52.         int[] phiconSmall = new int[1];  
  53.         uint count = ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);  
  54.         IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);  
  55.         resultIcon = Icon.FromHandle(IconHnd);  
  56.     }  
  57.   
  58.     catch  
  59.     {  
  60.         fileIcon = new string[] { systemDirectory + "shell32.dll""2" };  
  61.         //调用API方法读取图标  
  62.         int[] phiconLarge = new int[1];  
  63.         int[] phiconSmall = new int[1];  
  64.         uint count = ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);  
  65.         IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);  
  66.         resultIcon = Icon.FromHandle(IconHnd);  
  67.     }  
  68.     return resultIcon;  
  69. }  

另外还可以使用第一种方式API的SHGetFileInfo方法获取文件类型描述

[csharp]  view plain  copy
  1. /// <summary>           
  2. /// 获取指定文件的类型描述           
  3.  /// </summary>           
  4. /// <param name="fileName">文件名</param>           
  5. /// <returns>返回文件所属类型的类型描述</returns>           
  6. public static string GetTypeName(string fileName)  
  7. {  
  8.     string typeName = GetTypeName(fileName, FileAttribute.Normal, SHFileInfoFlags.UseFileAttributes | SHFileInfoFlags.TypeName | SHFileInfoFlags.DisplayName);  
  9.     if (string.IsNullOrEmpty(typeName))  
  10.         return "";  
  11.     else  
  12.         return typeName.Trim();  
  13. }  
  14.   
  15. /// <summary>           
  16. /// 获取文件类型           
  17.  /// </summary>           
  18. /// <param name="path">文件名</param>           
  19. /// <param name="dwAttr">文件信息</param>           
  20. /// <param name="dwFlag">信息控制字</param>          
  21. /// <returns></returns>           
  22. private static string GetTypeName(string path, FileAttribute dwAttr, SHFileInfoFlags dwFlag)  
  23. {  
  24.     try  
  25.     {  
  26.         SHFileInfo fi = new SHFileInfo();  
  27.         int iTotal = (int)SHGetFileInfo(path, dwAttr, ref fi, 0, dwFlag);  
  28.         if (iTotal > 0)  
  29.         {  
  30.             return fi.szTypeName;  
  31.         }  
  32.         else  
  33.         {  
  34.             return "";  
  35.         }  
  36.     }  
  37.     catch  
  38.     {  
  39.         return "";  
  40.     }  
  41. }  

另一种方式是使用注册表方式获取文件类型描述

[csharp]  view plain  copy
  1.  /// <summary>  
  2.  /// 获取文件扩展名说明  
  3.   /// </summary>  
  4. /// <param name="ext"></param>  
  5. /// <returns></returns>  
  6. public static string GetFileTypeDescription(string ext)  
  7. {  
  8.     RegistryKey rKey = null;  
  9.     RegistryKey sKey = null;  
  10.     string FileType = "";  
  11.     try  
  12.     {  
  13.         rKey = Registry.ClassesRoot;  
  14.         sKey = rKey.OpenSubKey(ext);  
  15.         if (sKey != null && (string)sKey.GetValue("", ext) != ext)  
  16.         {  
  17.             sKey = rKey.OpenSubKey((string)sKey.GetValue("", ext));  
  18.             FileType = (string)sKey.GetValue("");  
  19.         }  
  20.         else  
  21.         {  
  22.             FileType = ext.Substring(ext.LastIndexOf('.') + 1).ToUpper() + " File";  
  23.         }  
  24.         return FileType;  
  25.     }  
  26.     finally  
  27.     {  
  28.         if (sKey != null)  
  29.             sKey.Close();  
  30.         if (rKey != null)  
  31.             rKey.Close();  
  32.     }  
  33. }  
该示例类 下载
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值