I used the following code, taken of the code guru site which in turn took it off the msdn..
Its purpose is to provide you with the icon any particular file type is associated with.
and the following code in one of my procedures:
unfortionately the program breaks at:
System.Drawing.Icon myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon);
with the following error code:
An unhandled exception of type System.ArgumentException occurred in system.drawing.dll
Additional information: The Win32 handle you passed to Icon is invalid or of the wrong type.
I am not at all familiar with how the code is designmed, but maybe someone here is who can help me. Many thanx.
P.S. As I am writing a program that is likely to load the same icon in the imagelist multiple times. Is this a recipe for disaster or no problem? thanx
Its purpose is to provide you with the icon any particular file type is associated with.
Code:
[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;
};
class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // Large icon
public const uint SHGFI_SMALLICON = 0x1; // Small icon
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags);
}
and the following code in one of my procedures:
Code:
//deal with icon
IntPtr hImgSmall; //the handle to the system image list
IntPtr hImgLarge; //the handle to the system image list
string fName; // the file name to get icon from
SHFILEINFO shinfo = new SHFILEINFO();
lvw.SmallImageList = imageList1;
fName = path + "\\" + file.getName;
MessageBox.Show(fName);
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
Win32.SHGFI_ICON |
Win32.SHGFI_SMALLICON);
System.Drawing.Icon myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon);
imageList1.Images.Add(myIcon);
unfortionately the program breaks at:
System.Drawing.Icon myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon);
with the following error code:
An unhandled exception of type System.ArgumentException occurred in system.drawing.dll
Additional information: The Win32 handle you passed to Icon is invalid or of the wrong type.
I am not at all familiar with how the code is designmed, but maybe someone here is who can help me. Many thanx.
P.S. As I am writing a program that is likely to load the same icon in the imagelist multiple times. Is this a recipe for disaster or no problem? thanx