Tutorials Forums
     Tutorials Videos
        Sign Up Now For FREE
Welcome, Guest
Username Password: Remember me

How To Get File Icon(CSharp)
(1 viewing) (1) Guest
A programming language that evolved in part from Microsoft C++, C# was designed for building enterprise-level applications that run on the .NET Framework. C# is simple, modern, type safe, and object oriented. Whether you’re new to the language or an old pro, you’ll find articles in this section that will help you get your projects done.
  • Page:
  • 1

TOPIC: How To Get File Icon(CSharp)

How To Get File Icon(CSharp) 29 Jun 2010 21:26 #379

Introduction
In this article we will see how to Get File Icon.



Let’s get started:
Step 1:Start Visual studio and create a new Windows Forms Application
Step 2: Add Button,PictureBox and lable Control in Form
Step 3:Right Click the project and add a ‘Class’ , Rename Class To "GetFileIcon" and follow code's:
        public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; //Larg Icon
public const uint SHGFI_SMALLICON = 0x1; //Small icon
 
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(String pszpath,
uint dwFileAttibutes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags);
 
[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;
}
//
//Get larg Icon
//
public static System.Drawing.Icon GetLargIcon(String filename)
{
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr hImagelarg = SHGetFileInfo(filename, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
GetFileIcon.SHGFI_ICON | GetFileIcon.SHGFI_LARGEICON);
return System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
 
//
//Get Small Icon
//
public static System.Drawing.Icon GetSmallIcon(String filename)
{
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr hImagesmall = SHGetFileInfo(filename, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo), GetFileIcon.SHGFI_ICON|
GetFileIcon.SHGFI_SMALLICON);
return System.Drawing.Icon.FromHandle(shinfo.hIcon);
}

Step 4: Add the following code to the Button’s Click Event
   using (OpenFileDialog op = new OpenFileDialog())
{
if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.Drawing.Icon icon = GetFileIcon.GetLargIcon(op.FileName);
this.piclargicon.Image = (Image)icon.ToBitmap();
this.label1.Text = GetFileIcon.GetLargIcon(op.FileName).Width +
" X " +
GetFileIcon.GetLargIcon(op.FileName).Height;
}
}


5. Run project.

APPLIES TO

Microsoft Visual C# .NET 2002 Standard Edition
Microsoft Visual C# 2005 Express Edition
Microsoft Visual C# 2008 Express Edition
Microsoft Visual C# 2010 Express Edition


This attachment is hidden for guests. Please log in or register to see it. This attachment is hidden for guests. Please log in or register to see it.
Attachments:
  • Attachment This attachment is hidden for guests. Please log in or register to see it.
  • Attachment This attachment is hidden for guests. Please log in or register to see it.
  • CE
  • OFFLINE
  • Administrator
  • Posts: 197
  • Karma: 78
CodersEngine
  • Page:
  • 1
Moderators: mnjon
Time to create page: 0.42 seconds