C#中调用API获得指定文件的图标

发布时间:2009年06月04日      浏览次数:539 次
最近在写一个媒体管理的服务器程序。里面要用到了一个获得指定文件的集成图标的。所以就写了一个用C#中调用API来获得指定文件的图标的代码,代码如下:
导入API的类:
using System;
using System.Runtime.InteropServices;
namespace MediaClient
...{
public class Win32
...{
[DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
public static extern int ExtractIcon(IntPtr hInst,string lpFileName,int nIndex);
[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttribute,ref SHFILEINFO psfi,uint cbSizeFileInfo,uint Flags);
[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
...{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
}
}

调用方法:

/**//// <summary>
/// 获得指定路径下面的所有文件及文件夹
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public ArrayList GetListViewItem(string path,ImageList imglist)
...{
Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
try
...{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
ArrayList itemarr = new ArrayList();
for (int i = 0; i < dirs.Length; i++)
...{
string[] info = new string[4];
DirectoryInfo dir = new DirectoryInfo(dirs[i]);
//获得图标
Win32.SHGetFileInfo(dirs[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = dir.Name;
info[1] = "";
info[2] = "文件夹";
info[3] = dir.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,dir.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
for (int i = 0; i < files.Length; i++)
...{
string[] info = new string[4];
FileInfo fi = new FileInfo(files[i]);
//获得图标
Win32.SHGetFileInfo(files[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(fi.Name,(Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = fi.Name;
info[1] = fi.Length.ToString();
info[2] = fi.Extension.ToString();
info[3] = fi.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,fi.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
return itemarr;

}
catch
...{
return null;
}
}

uFlags常数:
SHGFI_ICON = 0x100
SHGFI_DISPLAYNAME = 0x200,
SHGFI_TYPENAME = 0x400,
SHGFI_ATTRIBUTES = 0x800,
SHGFI_ICONLOCATION = 0x1000,
SHGFI_EXETYPE = 0x2000,
SHGFI_SYSICONINDEX = 0x4000,
SHGFI_LINKOVERLAY = 0x8000,
SHGFI_SELECTED = 0x10000,
SHGFI_ATTR_SPECIFIED = 0x20000,
SHGFI_LARGEICON = 0x0,
SHGFI_SMALLICON = 0x1,
SHGFI_OPENICON = 0x2,
SHGFI_SHELLICONSIZE = 0x4,
SHGFI_PIDL = 0x8,
SHGFI_USEFILEATTRIBUTES = 0x10
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!