1,文件信息用FileInfo看 2,打开可以用二楼的办法。 3,对于获得文件类型的图标,目前的Framework可能只能用API获得吧^_^ 可以参考下面两个API // [DllImport("Shell32.dll")] public static extern int ExtractIcon(IntPtr h,string strx,int ii); [DllImport("Shell32.dll")] public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags); public struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; public char szDisplayName; public char szTypeName; }
// For extracting the icons from the Shell DLL, so we get native icons in every OS version. // XP and up should use a manifest file. [DllImport("Shell32.dll",EntryPoint="ExtractIconExW",CharSet=CharSet.Unicode, ExactSpelling=true,CallingConvention=CallingConvention.StdCall)] public static extern int ExtractIconEx(string sFile,int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons); // 在Windows系统中, 每一中后缀的文件都可以有相应的图标(Icon). 其中, EXE文件和DLL文件及ICO文件, 图标是内置的. 其它文件基本上是使用打开它们的执行程序的图标. 对陌生的文件类型, 系统也指定了缺省图标. 以上设定, 可以在注册表中按步骤实现. 首先由文件后缀来找到它的ProgID, 如"RealPlayer.MP3.6", "exefile", "jpegfile"等. 再根据ProgID寻找它的DefaultIcon 例: E:\WINNT\Installer\{00000409-78E1-11D2-B60F-006097C998E7}\PEicons.exe,5 其中逗号前是Icon所在的文件, 逗号后的数字是Icon索引号. 根据这两个参数就可以提取出Icon对象.
我正在用的代码(VB.NET,很容易转换为C#),很好用,仅供参考:Imports System.Runtime.InteropServices Module AssociatedIcon Private Structure SHFILEINFO Public hIcon As IntPtr ' : icon Public iIcon As Integer ' : icondex Public dwAttributes As Integer ' : SFGAO_ flags <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _ Public szDisplayName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _ Public szTypeName As String End Structure Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, _ ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, _ ByVal uFlags As Integer) As IntPtr Private Const SHGFI_ICON As Integer = &H100 Private Const SHGFI_SMALLICON As Integer = &H1 Private Const SHGFI_LARGEICON As Integer = &H0 ' Large icon Public Function GetAssociatedIcon(ByVal asFilename As String) As Icon Dim shinfo As SHFILEINFO Dim hImgSmall As IntPtr 'The handle to the system image list. 'Dim hImgLarge As IntPtr 'The handle to the system image list. shinfo = New SHFILEINFO() shinfo.szDisplayName = New String(Chr(0), 260) shinfo.szTypeName = New String(Chr(0), 80) 'Use this to get the small icon. hImgSmall = SHGetFileInfo(asFilename, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON) 'Use this to get the large icon. 'hImgLarge = SHGetFileInfo(asFilename, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON) 'The icon is returned in the hIcon member of the shinfo struct. Return Icon.FromHandle(shinfo.hIcon) End Function End Module调用: Me.TheImageList.Images.Add(AssociatedIcon.GetAssociatedIcon(asExecutable)) Me.ThePictureBox.Image = Me.TheImageList.Images(0) 其中 asExecutable 为可执行文件名。
2,打开可以用二楼的办法。
3,对于获得文件类型的图标,目前的Framework可能只能用API获得吧^_^
可以参考下面两个API
//
[DllImport("Shell32.dll")]
public static extern int ExtractIcon(IntPtr h,string strx,int ii); [DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags); public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public char szDisplayName;
public char szTypeName;
}
// XP and up should use a manifest file.
[DllImport("Shell32.dll",EntryPoint="ExtractIconExW",CharSet=CharSet.Unicode, ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
public static extern int ExtractIconEx(string sFile,int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons); //
在Windows系统中, 每一中后缀的文件都可以有相应的图标(Icon).
其中, EXE文件和DLL文件及ICO文件, 图标是内置的.
其它文件基本上是使用打开它们的执行程序的图标.
对陌生的文件类型, 系统也指定了缺省图标.
以上设定, 可以在注册表中按步骤实现.
首先由文件后缀来找到它的ProgID,
如"RealPlayer.MP3.6", "exefile", "jpegfile"等.
再根据ProgID寻找它的DefaultIcon
例: E:\WINNT\Installer\{00000409-78E1-11D2-B60F-006097C998E7}\PEicons.exe,5
其中逗号前是Icon所在的文件, 逗号后的数字是Icon索引号.
根据这两个参数就可以提取出Icon对象.
有时没有DefaultIcon键值, 也可以从CLSID再另行查找Icon所在的文件.
以上操作如果没有找到合适的Icon, (如.dat文件或无后缀文件等), 就从kernal32.dll中取系统默认的Icon.
另外: Icon还有32*32, 16*16两种模式, 可以一起提取出来用于不同的View方式.
除非你程序打开时,自动把注册表改了
这样也太不友好了
Module AssociatedIcon
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON As Integer = &H100
Private Const SHGFI_SMALLICON As Integer = &H1
Private Const SHGFI_LARGEICON As Integer = &H0 ' Large icon
Public Function GetAssociatedIcon(ByVal asFilename As String) As Icon Dim shinfo As SHFILEINFO
Dim hImgSmall As IntPtr 'The handle to the system image list.
'Dim hImgLarge As IntPtr 'The handle to the system image list. shinfo = New SHFILEINFO()
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80) 'Use this to get the small icon.
hImgSmall = SHGetFileInfo(asFilename, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON) 'Use this to get the large icon.
'hImgLarge = SHGetFileInfo(asFilename, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON) 'The icon is returned in the hIcon member of the shinfo struct.
Return Icon.FromHandle(shinfo.hIcon) End Function
End Module调用:
Me.TheImageList.Images.Add(AssociatedIcon.GetAssociatedIcon(asExecutable))
Me.ThePictureBox.Image = Me.TheImageList.Images(0)
其中 asExecutable 为可执行文件名。