Click here to Skip to main content
15,937,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i've used SHGetFileInfo with SHGFI_USEFILEATTRIBUTES for uFlag
but also it shows error
any idea please.this code is given in internet to get the icon of a file from ftp using its extension but it shows error.
it shows error "Win32 handle that was passed to Icon is not valid or is the wrong type".
see whether the code is wrong

VB
<StructLayout(LayoutKind.Sequential)> _
Public Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure

Public Declare Ansi Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, _
ByVal dwFileAttributes As Form1.FileAttributes, ByRef psfi As SHFILEINFO, ByVal 
cbFileInfo As UInt32, _
ByVal uFlags As FileInformationFlags) As IntPtr

<Flags()> _
Public Enum FileAttributes As UInt32
FILE_ATTRIBUTE_ARCHIVE = &H20
FILE_ATTRIBUTE_COMPRESSED = &H800
<EditorBrowsable(EditorBrowsableState.
Never)> _
FILE_ATTRIBUTE_DEVICE = &H40
FILE_ATTRIBUTE_DIRECTORY = &H10
FILE_ATTRIBUTE_ENCRYPTED = &H4000
FILE_ATTRIBUTE_HIDDEN = &H2
FILE_ATTRIBUTE_NORMAL = &H80
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
FILE_ATTRIBUTE_OFFLINE = &H1000
FILE_ATTRIBUTE_READONLY = &H1
FILE_ATTRIBUTE_REPARSE_POINT = &H400
FILE_ATTRIBUTE_SPARSE_FILE = &H200
FILE_ATTRIBUTE_SYSTEM = &H4
FILE_ATTRIBUTE_TEMPORARY = &H100
FILE_ATTRIBUTE_VIRTUAL = &H10000
End Enum

<Flags()> _
Public Enum FileInformationFlags As UInt32
<EditorBrowsable(EditorBrowsableState.
Advanced)> _
zeor = 0UI
SHGFI_ADDOVERLAYS = &H20UI
SHGFI_ATTR_SPECIFIED = &H20000UI
SHGFI_ATTRIBUTES = &H800UI
SHGFI_DISPLAYNAME = &H200UI
SHGFI_EXETYPE = &H2000UI
SHGFI_ICON = &H100UI
SHGFI_ICONLOCATION = &H1000UI
SHGFI_LARGEICON = &H0UI
SHGFI_LINKOVERLAY = &H8000UI
SHGFI_OPENICON = &H2UI
SHGFI_OVERLAYINDEX = &H40UI
SHGFI_PIDL = &H8UI
SHGFI_SELECTED = &H10000UI
SHGFI_SHELLICONSIZE = &H4UI
SHGFI_SMALLICON = &H1UI
SHGFI_SYSICONINDEX = &H4000UI
SHGFI_TYPENAME = &H400UI
SHGFI_USEFILEATTRIBUTES = &H10UI
End Enum

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button1.Click
Dim hImgSmall As IntPtr
Dim hImgLarge As IntPtr
Dim fName As String
Dim shinfo As SHFILEINFO
shinfo = New SHFILEINFO()

'fName = st
'fName = s4(i) 'full file path
Dim st1 As String = st.Substring(st.LastIndexOf("."))
fName = Form2.FolderBrowserDialog1.SelectedPath + "\du" + st1
MsgBox(fName)
hImgLarge = SHGetFileInfo(fName, 
FileAttributes.FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo),
FileInformationFlags.SHGFI_USEFILEATTR…

Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.
FromHandle(shinfo.hIcon)

m.PictureBox1.Image = myIcon.ToBitmap
m.PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage

End Sub


Edit TR - Please put you code in <pre> tags, it makes it easier to read.
Posted
Updated 6-Apr-10 3:30am
v3

1 solution

The answer is quite simple:

VB
Dim sProg As String
Dim sFileExt As String = ".pdf"

tmp = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(sFileExt).GetValue("")
' Get the program that will open files with this extension
sProg = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(tmp).OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command").GetValue("")

' strip the filename
If sProg.Substring(0, 1) = Chr(34) Then
    sProg = sProg.Substring(1, sProg.IndexOf(Chr(34), 2) - 1)
Else
    sProg = sProg.Substring(0, sProg.IndexOf(" ", 2))
End If
sProg = Replace(sProg, "%1", "")

' Extract the icon from the program

oIcon = System.Drawing.Icon.ExtractAssociatedIcon(sProg)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900