public class ShellImages
{
#region DLLIMPORT
// Retrieves information about an object in the file system,
// such as a file, a folder, a directory, or a drive root.
[DllImport("shell32",
EntryPoint = "SHGetFileInfo",
ExactSpelling = false,
CharSet = CharSet.Auto,
SetLastError = true)]
private static extern IntPtr SHGetFileInfo(
string pszPath, //指定的文件名
FILE_ATTRIBUTE dwFileAttributes, //文件属性
ref SHFILEINFO sfi, //返回获得的文件信息,是一个记录类型
using System;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace TOA.Common
{
/// <summary>
/// GetIcon 的摘要说明。
/// </summary>
public class ShellImages
{
#region DLLIMPORT
// Retrieves information about an object in the file system,
// such as a file, a folder, a directory, or a drive root.
[DllImport("shell32",
EntryPoint = "SHGetFileInfo",
ExactSpelling = false,
CharSet = CharSet.Auto,
SetLastError = true)]
private static extern IntPtr SHGetFileInfo(
string pszPath, //指定的文件名
FILE_ATTRIBUTE dwFileAttributes, //文件属性
ref SHFILEINFO sfi, //返回获得的文件信息,是一个记录类型
int cbFileInfo, //文件的类型名
SHGFI uFlags);
#endregion
#region STRUCTS
// Contains information about a file object
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private 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; //文件的类型名
};
#endregion
#region Enums
// Flags that specify the file information to retrieve with SHGetFileInfo
[Flags]
public enum SHGFI : uint
{
ADDOVERLAYS = 0x20,
ATTR_SPECIFIED = 0x20000,
ATTRIBUTES = 0x800, //获得属性
DISPLAYNAME = 0x200, //获得显示名
EXETYPE = 0x2000,
ICON = 0x100, //获得图标
ICONLOCATION = 0x1000,
LARGEICON = 0, //获得大图标
LINKOVERLAY = 0x8000,
OPENICON = 2,
OVERLAYINDEX = 0x40,
PIDL = 8,
SELECTED = 0x10000,
SHELLICONSIZE = 4,
SMALLICON = 1, //获得小图标
SYSICONINDEX = 0x4000,
TYPENAME = 0x400, //获得类型名
USEFILEATTRIBUTES = 0x10
}
// Flags that specify the file information to retrieve with SHGetFileInfo
[Flags]
public enum FILE_ATTRIBUTE
{
READONLY = 0x00000001,
HIDDEN = 0x00000002,
SYSTEM = 0x00000004,
DIRECTORY = 0x00000010,
ARCHIVE = 0x00000020,
DEVICE = 0x00000040,
NORMAL = 0x00000080,
TEMPORARY = 0x00000100,
SPARSE_FILE = 0x00000200,
REPARSE_POINT = 0x00000400,
COMPRESSED = 0x00000800,
OFFLINE = 0x00001000,
NOT_CONTENT_INDEXED = 0x00002000,
ENCRYPTED = 0x00004000
}
#endregion
#region Variables
//保存小图标列表
private static System.Windows.Forms.ImageList smallImageList;
//保存大图标列表
private static System.Windows.Forms.ImageList largeImageList;
static ShellImages()
{
smallImageList = new System.Windows.Forms.ImageList();
largeImageList = new System.Windows.Forms.ImageList();
//将 ImageList中的图标设置为32位色图标,这样可以得到较好的显示效果
smallImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
largeImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
largeImageList.ImageSize = new Size(32, 32);
addForderIcon();
}
#endregion
#region PrivateMethods
/// <summary>
/// 获取系统图标
/// </summary>
/// <param name="path">文件名</param>
/// <param name="dwAttr">文件信息</param>
/// <param name="dwFlag">获取信息控制字</param>
/// <returns></returns>
private static Icon GetIcon(string path, FILE_ATTRIBUTE dwAttr, SHGFI dwFlag)
{
SHFILEINFO fi = new SHFILEINFO();
Icon ic = null;
int iTotal = (int)SHGetFileInfo(path, dwAttr, ref fi, 0, dwFlag);
ic = Icon.FromHandle(fi.hIcon);
return ic;
}
/// <summary>
/// 向smallInamgeList和largeImageList中
/// 加入相应文件对应的图标
/// </summary>
/// <param name="fileName"></param>
private static void addFileIcon(string fileName)
{
smallImageList.Images.Add(GetIcon(fileName,
FILE_ATTRIBUTE.NORMAL,
SHGFI.USEFILEATTRIBUTES | SHGFI.ICON | SHGFI.LARGEICON));
largeImageList.Images.Add(GetIcon(fileName,
FILE_ATTRIBUTE.NORMAL,
SHGFI.USEFILEATTRIBUTES | SHGFI.ICON | SHGFI.LARGEICON));
}
private static void addForderIcon()
{
smallImageList.Images.Add(GetIcon("dic",
FILE_ATTRIBUTE.DIRECTORY,
SHGFI.USEFILEATTRIBUTES | SHGFI.ICON | SHGFI.LARGEICON));
largeImageList.Images.Add(GetIcon("dic",
FILE_ATTRIBUTE.DIRECTORY,
SHGFI.USEFILEATTRIBUTES | SHGFI.ICON | SHGFI.LARGEICON));
}
#endregion
#region PublicMethods
/// <summary>
/// 获取系统的小图标列表
/// </summary>
public static System.Windows.Forms.ImageList SmallImageList
{
get
{
return smallImageList;
}
}
/// <summary>
/// 获取系统的大图标列表
/// </summary>
public static System.Windows.Forms.ImageList LargeImageList
{
get
{
return largeImageList;
}
}
/// <summary>
/// 文件对应的图标在ImageList中的索引号
/// (在SmallImageList,LargeImageList中的索引号是相同的)
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns>索引号</returns>
public static int ImageIndex(string fileName)
{
int index=0;
int extIndex = fileName.LastIndexOf('.');
if (extIndex < 0)//没有扩展名的文件当作文件夹处理
{
extIndex = 2;
}
else//文件图标索引获取
{
fileName = fileName.Substring(extIndex, fileName.Length - extIndex);
//判断该扩展名的文件图标是否已装载到imageList中
addFileIcon(fileName);//装载图标到ImageList中
index = smallImageList.Images.Count - 1;
}
return index;
}
#endregion
}
}
接着再在窗体的LOAD事件里写:
#region"窗体_Load 事件"
/// <summary>
/// 窗体_Load 事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileList_Load(object sender, System.EventArgs e)
{
lvFiles.SmallImageList = ShellImages.SmallImageList;
lvFiles.LargeImageList = ShellImages.LargeImageList;
}
#endregion
其中:lvFiles是ListView控件
将下列代码加到需要的地方即可:
其中:dlgOpenFile是dialogFileOpen(文件打开对话框)控件
string strFileName = comFunction.GetNumberCD();
dlgOpenFile.CheckPathExists = true;
dlgOpenFile.CheckFileExists = true;
dlgOpenFile.Multiselect = false;
dlgOpenFile.Filter = "All files|*.*";
if(dlgOpenFile.ShowDialog() == DialogResult.OK)
{
statusBarPanel1.Text = "文件加载中……";
FileInfo myFiles = new FileInfo(dlgOpenFile.FileName);
FileStream FStream = myFiles.OpenRead();
ListViewItem lvItem = new ListViewItem(myFiles.Name);
lvItem.SubItems.Add(myFiles.Extension.ToUpper());
lvItem.SubItems.Add(myFiles.Length.ToString()+"字节");
lvItem.SubItems.Add(myFiles.CreationTime.ToString("yyyy/MM/dd hh:mm:ss"));
lvItem.SubItems.Add(strFileName);
lvItem.ImageIndex = ShellImages.ImageIndex(lvItem.Text);
lvFiles.Items.Add(lvItem);
DateFjIns(strFileName,myFiles,FStream);
FStream.Close();
statusBarPanel1.Text = dlgOpenFile.FileName;;
}
dlgOpenFile.Dispose();
以上操作可以完成自动读取出系统文件图标,并将其加到ListView中