/// <summary> /// 获取显示名称。 /// </summary> /// <param name="fileName">文件名(如:win.rar;setup.exe;temp.txt)</param> /// <returns>显示名称。</returns> public static string GetDisplayName(string fileName) { Thinksea.Windows.Win32API.SHFILEINFO fileInfo = new Thinksea.Windows.Win32API.SHFILEINFO(); //初始化FileInfomation结构 //调用GetFileInfo函数,最后一个参数说明获取的是文件类型(SHGFI_TYPENAME) System.IntPtr res = Thinksea.Windows.Win32API.User32.SHGetFileInfo(fileName, (uint)Thinksea.Windows.Win32API.FileAttributeFlags.FILE_ATTRIBUTE_NORMAL, ref fileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(fileInfo), (uint)Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_DISPLAYNAME); return(fileInfo.szTypeName); }
/// <summary> /// 根据文件扩展名得到系统扩展名的图标 /// </summary> /// <param name="fileName">文件名(如:win.rar;setup.exe;temp.txt)</param> /// <param name="largeIcon">图标的大小</param> /// <returns></returns> public static System.Drawing.Icon GetFileIcon(string fileName, bool largeIcon) { Thinksea.Windows.Win32API.SHFILEINFO _SHFILEINFO = new Thinksea.Windows.Win32API.SHFILEINFO(); Thinksea.Windows.Win32API.GetFileInfoFlags flags; if (largeIcon) { flags = Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_ICON | Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_LARGEICON | Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_USEFILEATTRIBUTES; } else { flags = Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_ICON | Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_SMALLICON | Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_USEFILEATTRIBUTES; } System.IntPtr IconIntPtr = Thinksea.Windows.Win32API.User32.SHGetFileInfo(fileName, (uint)Thinksea.Windows.Win32API.FileAttributeFlags.FILE_ATTRIBUTE_TEMPORARY, ref _SHFILEINFO, (uint)System.Runtime.InteropServices.Marshal.SizeOf(_SHFILEINFO), (uint)flags); if (IconIntPtr.Equals(System.IntPtr.Zero)) { return(null); } return(System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon)); }
/// <summary> /// 获取目录图标 /// </summary> /// <param name="largeIcon">指示获取大图标还是小图标。</param> /// <returns>图标</returns> public static System.Drawing.Icon GetDirectoryIcon(bool largeIcon) { Thinksea.Windows.Win32API.SHFILEINFO _SHFILEINFO = new Thinksea.Windows.Win32API.SHFILEINFO(); Thinksea.Windows.Win32API.GetFileInfoFlags flags; if (largeIcon) { flags = Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_ICON | Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_LARGEICON; } else { flags = Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_ICON | Thinksea.Windows.Win32API.GetFileInfoFlags.SHGFI_SMALLICON; } System.IntPtr IconIntPtr = Thinksea.Windows.Win32API.User32.SHGetFileInfo(@"", (uint)0, ref _SHFILEINFO, (uint)System.Runtime.InteropServices.Marshal.SizeOf(_SHFILEINFO), (uint)flags); if (IconIntPtr.Equals(System.IntPtr.Zero)) { return(null); } System.Drawing.Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon); return(_Icon); }