using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; namespace ShowIcon { [StructLayout( LayoutKind.Sequential)] public struct FileInfomation { public IntPtr hIcon; public int iIcon; public int dwAttributes;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst = 260 )] public string szDisplayName;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst = 80 )] public string szTypeName; }
/// <summary> /// Description of MainForm. /// </summary> public partial class MainForm { [STAThread] public static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); }
[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")] public static extern int GetFileInfo(string pszPath, int dwFileAttributes, ref FileInfomation psfi, int cbFileInfo,int uFlags); public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent();
// // TODO: Add constructor code after the InitializeComponent() call. // } public static Icon GetSmallIcon(string path) { FileInfomation _info = new FileInfomation();
GetFileInfo(path, 0, ref _info, Marshal.SizeOf(_info), (int)(0x000000100| 0x000000001)); try { return Icon.FromHandle(_info.hIcon); } catch { return null; } } void Button1Click(object sender, System.EventArgs e) { this.openFileDialog1.ShowDialog(); }
void OpenFileDialog1FileOk(object sender, System.ComponentModel.CancelEventArgs e) { this.textBox1.Text=this.openFileDialog1.FileName; this.pictureBox1.Image=GetSmallIcon(this.openFileDialog1.FileName).ToBitmap(); } }
}
|
请发表评论