如何获取文件在系统中的图标?

None.gif
None.gif
public   class  SysIO2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public SysIO2()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
private const uint SHGFI_ICON = 0x100;
InBlock.gif    
private const uint SHGFI_LARGEICON = 0x0;
InBlock.gif    
private const uint SHGFI_SMALLICON = 0x1;
InBlock.gif    
public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
InBlock.gif
InBlock.gif
InBlock.gif    [DllImport(
"kernel32.dll")]
InBlock.gif    
internal static extern void ExitProcess(int a);
InBlock.gif
InBlock.gif    [DllImport(
"shell32.dll")]
InBlock.gif    
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref   SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
InBlock.gif
InBlock.gif    [StructLayout(LayoutKind.Sequential)]
InBlock.gif    
private struct SHFILEINFO
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public IntPtr hIcon;
InBlock.gif        
public IntPtr iIcon;
InBlock.gif        
public uint dwAttributes;
InBlock.gif        [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
= 260)]
InBlock.gif        
public string szDisplayName;
InBlock.gif        [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
= 80)]
InBlock.gif        
public string szTypeName;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取指定文件(或者扩展名)和系统关联的小图标
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="strExtension">文件名,或者文件扩展名(.bmp等等)</param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    internal static Icon GetSmallIcon(string strExtension)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strFileName = Path.GetExtension(strExtension);
InBlock.gif        
if (strFileName == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strFileName 
= strExtension;
ExpandedSubBlockEnd.gif        }

InBlock.gif        IntPtr hImgSmall;
InBlock.gif        SHFILEINFO shinfo 
= new SHFILEINFO();
InBlock.gif        hImgSmall 
= SHGetFileInfo(strFileName, 0ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
InBlock.gif        Icon myIcon 
= System.Drawing.Icon.FromHandle(shinfo.hIcon);
InBlock.gif        
return myIcon;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取指定文件(或者扩展名)和系统关联的大图标
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="strExtension">文件名,或者文件扩展名(.bmp等等)</param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    internal static Icon GetLargeIcon(string strExtension)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strFileName = Path.GetExtension(strExtension);
InBlock.gif        
if (strFileName == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strFileName 
= strExtension;
ExpandedSubBlockEnd.gif        }

InBlock.gif        IntPtr hImgLarge;
InBlock.gif        SHFILEINFO shinfo 
= new SHFILEINFO();
InBlock.gif        hImgLarge 
= SHGetFileInfo(strFileName, 0ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES);
InBlock.gif        Icon myIcon 
= System.Drawing.Icon.FromHandle(shinfo.hIcon);
InBlock.gif        
return myIcon;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/dreign/archive/2007/01/22/627146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值