首先明確問題:獲取一個文件的關聯圖標或者是某個類型文件的顯示圖標。
在網上搜了一圈,發現方法還是比較多的,但是應用C#進行獲取的方法不多。我選擇一種用.Net庫的方法。
使用的類:
System.Drawing.Icon ,位於System.Drawing 命名空間。
具體方法:
System.Drawing.Icon 類中的靜態方法:public static Icon ExtractAssociatedIcon(string filePath)
此方法接收一個文件路徑,返回一個 Icon 類型的對象。此對象既是指定文件的 Icon 圖標對象。
此 Icon 類提供了 Save 方法,可以將 Icon 文件寫入流中,進而保存至硬盤文件。
當然要在程序中顯示也是可以的,Icon 類提供了 ToBitmap 方法,可以將 Icon 文件轉換為 Bitmap。Bitmap 就可以在 PictureBox 中顯示了。
如果要根據後綴名來獲取文件圖標,那要怎麼辦呢?我想了一個比較笨的辦法。看代碼~
string
fileName =
"tmp."
+ houzhuiming;
File.Create(fileName).Close();
Image img = System.Drawing.Icon.ExtractAssociatedIcon(fileName).ToBitmap();
File.Delete(fileName);
如此,新建一個指定後綴名的文件,獲取圖標後直接刪除,就能獲得指定文件後綴名的圖標了。嘻嘻~
下面給大家分享一段代碼介紹C#獲取文件關聯圖標
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Runtime.InteropServices;
using
System.Drawing;
using
Microsoft.Win32;
namespace
ifmdp.DeviceManager
{
class
SystemIcon
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public
struct
SHFILEINFO
{
public
IntPtr hIcon;
public
int
iIcon;
public
uint
dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public
string
szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public
string
szTypeName;
}
[DllImport(
"Shell32.dll"
, EntryPoint =
"SHGetFileInfo"
, SetLastError =
true
, CharSet = CharSet.Auto)]
public
static
extern
IntPtr SHGetFileInfo(
string
pszPath,
uint
dwFileAttributes,
ref
SHFILEINFO psfi,
uint
cbFileInfo,
uint
uFlags);
[DllImport(
"User32.dll"
, EntryPoint =
"DestroyIcon"
)]
public
static
extern
int
DestroyIcon(IntPtr hIcon);
#region API 參數的常量定義
public
enum
FileInfoFlags :
uint
{
SHGFI_ICON = 0x000000100,
// get icon
SHGFI_DISPLAYNAME = 0x000000200,
// get display name
SHGFI_TYPENAME = 0x000000400,
// get type name
SHGFI_ATTRIBUTES = 0x000000800,
// get attributes
SHGFI_ICONLOCATION = 0x000001000,
// get icon location
SHGFI_EXETYPE = 0x000002000,
// return exe type
SHGFI_SYSICONINDEX = 0x000004000,
// get system icon index
SHGFI_LINKOVERLAY = 0x000008000,
// put a link overlay on icon
SHGFI_SELECTED = 0x000010000,
// show icon in selected state
SHGFI_ATTR_SPECIFIED = 0x000020000,
// get only specifIEd attributes
SHGFI_LARGEICON = 0x000000000,
// get large icon
SHGFI_SMALLICON = 0x000000001,
// get small icon
SHGFI_OPENICON = 0x000000002,
// get open icon
SHGFI_SHELLICONSIZE = 0x000000004,
// get shell size icon
SHGFI_PIDL = 0x000000008,
// pszPath is a pidl
SHGFI_USEFILEATTRIBUTES = 0x000000010,
// use passed dwFileAttribute
SHGFI_ADDOVERLAYS = 0x000000020,
// apply the appropriate overlays
SHGFI_OVERLAYINDEX = 0x000000040
// Get the index of the overlay
}
public
enum
FileAttributeFlags :
uint
{
FILE_ATTRIBUTE_READONLY = 0x00000001,
FILE_ATTRIBUTE_HIDDEN = 0x00000002,
FILE_ATTRIBUTE_SYSTEM = 0x00000004,
FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
FILE_ATTRIBUTE_DEVICE = 0x00000040,
FILE_ATTRIBUTE_NORMAL = 0x00000080,
FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
FILE_ATTRIBUTE_OFFLINE = 0x00001000,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
FILE_ATTRIBUTE_ENCRYPTED = 0x00004000
}
#endregion
/// <summary>
/// 獲取文件類型的關聯圖標
/// </summary>
/// <param name="fileName">文件類型的擴展名或文件的絕對路徑</param>
/// <param name="isLargeIcon">是否返回大圖標</param>
/// <returns>獲取到的圖標</returns>
public
static
Icon GetIcon(
string
fileName,
bool
isLargeIcon)
{
SHFILEINFO shfi =
new
SHFILEINFO();
IntPtr hI;
if
(isLargeIcon)
hI = SHGetFileInfo(fileName, 0,
ref
shfi, (
uint
)Marshal.SizeOf(shfi), (
uint
)FileInfoFlags.SHGFI_ICON | (
uint
)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (
uint
)FileInfoFlags.SHGFI_LARGEICON);
else
hI = SHGetFileInfo(fileName, 0,
ref
shfi, (
uint
)Marshal.SizeOf(shfi), (
uint
)FileInfoFlags.SHGFI_ICON | (
uint
)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (
uint
)FileInfoFlags.SHGFI_SMALLICON);
Icon icon = Icon.FromHandle(shfi.hIcon).Clone()
as
Icon;
DestroyIcon(shfi.hIcon);
//釋放資源
return
icon;
}
/// <summary>
/// 獲取文件夾圖標
/// </summary>
/// <returns>圖標</returns>
public
static
Icon GetDirectoryIcon(
bool
isLargeIcon)
{
SHFILEINFO _SHFILEINFO =
new
SHFILEINFO();
IntPtr _IconIntPtr;
if
(isLargeIcon)
{
_IconIntPtr = SHGetFileInfo(
@""
, 0,
ref
_SHFILEINFO, (
uint
)Marshal.SizeOf(_SHFILEINFO), ((
uint
)FileInfoFlags.SHGFI_ICON | (
uint
)FileInfoFlags.SHGFI_LARGEICON));
}
else
{
_IconIntPtr = SHGetFileInfo(
@""
, 0,
ref
_SHFILEINFO, (
uint
)Marshal.SizeOf(_SHFILEINFO), ((
uint
)FileInfoFlags.SHGFI_ICON | (
uint
)FileInfoFlags.SHGFI_SMALLICON));
}
if
(_IconIntPtr.Equals(IntPtr.Zero))
return
null
;
Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
return
_Icon;
}
}
}