473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get associated icon for a file

Hi all,

1) I found out from the vb.net group that it is possible to get an
associated icon for a file. My requirement is to display all programs
from the Start Menu for the user along with the associated icons. While
I am able to list the programs correctly, the icons that get displayed
alongside are the ones for the folders.
The form has listview and imagelist controls on it. I shall paste the
code here.
2) I was also wondering why this piece of code ,
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription
returns the description for an executable file only. I am trying to
display the program description instead of the the path for the files.
Please help. Thanks in advance.
'************************************************* **********************************************
Imports System.Runtime.InteropServices
Imports System.IO

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents ColumnHeader1 As
System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.ListView1 = New System.Windows.Forms.ListView
Me.ImageList1 = New
System.Windows.Forms.ImageList(Me.components)
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ListView1
'
Me.ListView1.Columns.AddRange(New
System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1})
Me.ListView1.Location = New System.Drawing.Point(8, 40)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(816, 368)
Me.ListView1.TabIndex = 1
Me.ListView1.View = System.Windows.Forms.View.Details
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.TransparentColor =
System.Drawing.Color.Transparent
'
'ColumnHeader1
'
Me.ColumnHeader1.Width = 812
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(872, 438)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure

Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll"
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private nIndex = 0

Dim intI As Integer = 0

'************************************************* **********************************
'Retrieve and set the large and small ImageLists for ListView1 at
form load
'************************************************* **********************************

Sub DirSearch(ByVal sDir As String, ByVal obListView As ListView)
Dim d As String
Dim f As String
'-------------------------------------------------------
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim hImgLarge As IntPtr 'The handle to the system image list.
Dim fName As String 'The file name to get the icon from.
Dim shinfo As New SHFILEINFO
shinfo = New SHFILEINFO

Dim SHI As New SHFILEINFO

ListView1.SmallImageList = ImageList1
ListView1.LargeImageList = ImageList1

shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
'----------------------------------------------------------
Dim lvItem As ListViewItem
'ListView item.
Dim myIcon As System.Drawing.Icon
Dim strfDesc As String

Try
For Each d In Directory.GetDirectories(sDir)
For Each f In Directory.GetFiles(d, "*.*")
intI = intI + 1
With obListView
.BeginUpdate()
''---------------------------------------------------
'Use this to get the small icon.
hImgSmall = SHGetFileInfo(fName, 0, shinfo,
Marshal.SizeOf(shinfo), _
SHGFI_ICON Or SHGFI_SMALLICON)
myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon)
ImageList1.Images.Add(myIcon) 'Add icon to
imageList.
''---------------------------------------------------
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription

.Items.Add(f, nindex)
nIndex = nIndex + 1
.EndUpdate()
End With
Next
DirSearch(d, obListView)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strWinDir As String
strWinDir =
Environment.GetFolderPath(Environment.SpecialFolde r.StartMenu)

DirSearch(strWinDir, ListView1)

End Sub
End Class

Jan 24 '06 #1
4 3809
http://www.codeguru.com/vb/gen/vb_mi...cle.php/c5597/

:)

Mythran

"techspirit" <te********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi all,

1) I found out from the vb.net group that it is possible to get an
associated icon for a file. My requirement is to display all programs
from the Start Menu for the user along with the associated icons. While
I am able to list the programs correctly, the icons that get displayed
alongside are the ones for the folders.
The form has listview and imagelist controls on it. I shall paste the
code here.
2) I was also wondering why this piece of code ,
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription
returns the description for an executable file only. I am trying to
display the program description instead of the the path for the files.
Please help. Thanks in advance.
'************************************************* **********************************************
Imports System.Runtime.InteropServices
Imports System.IO

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents ColumnHeader1 As
System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.ListView1 = New System.Windows.Forms.ListView
Me.ImageList1 = New
System.Windows.Forms.ImageList(Me.components)
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ListView1
'
Me.ListView1.Columns.AddRange(New
System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1})
Me.ListView1.Location = New System.Drawing.Point(8, 40)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(816, 368)
Me.ListView1.TabIndex = 1
Me.ListView1.View = System.Windows.Forms.View.Details
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.TransparentColor =
System.Drawing.Color.Transparent
'
'ColumnHeader1
'
Me.ColumnHeader1.Width = 812
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(872, 438)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure

Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll"
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private nIndex = 0

Dim intI As Integer = 0

'************************************************* **********************************
'Retrieve and set the large and small ImageLists for ListView1 at
form load
'************************************************* **********************************

Sub DirSearch(ByVal sDir As String, ByVal obListView As ListView)
Dim d As String
Dim f As String
'-------------------------------------------------------
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim hImgLarge As IntPtr 'The handle to the system image list.
Dim fName As String 'The file name to get the icon from.
Dim shinfo As New SHFILEINFO
shinfo = New SHFILEINFO

Dim SHI As New SHFILEINFO

ListView1.SmallImageList = ImageList1
ListView1.LargeImageList = ImageList1

shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
'----------------------------------------------------------
Dim lvItem As ListViewItem
'ListView item.
Dim myIcon As System.Drawing.Icon
Dim strfDesc As String

Try
For Each d In Directory.GetDirectories(sDir)
For Each f In Directory.GetFiles(d, "*.*")
intI = intI + 1
With obListView
.BeginUpdate()
''---------------------------------------------------
'Use this to get the small icon.
hImgSmall = SHGetFileInfo(fName, 0, shinfo,
Marshal.SizeOf(shinfo), _
SHGFI_ICON Or SHGFI_SMALLICON)
myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon)
ImageList1.Images.Add(myIcon) 'Add icon to
imageList.
''---------------------------------------------------
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription

.Items.Add(f, nindex)
nIndex = nIndex + 1
.EndUpdate()
End With
Next
DirSearch(d, obListView)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strWinDir As String
strWinDir =
Environment.GetFolderPath(Environment.SpecialFolde r.StartMenu)

DirSearch(strWinDir, ListView1)

End Sub
End Class


Jan 24 '06 #2
"techspirit" <te********@gmail.com> schrieb:
2) I was also wondering why this piece of code ,
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription
returns the description for an executable file only. I am trying to
display the program description instead of the the path for the files.


Determining the description of a file's file type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=getfiledescription&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jan 24 '06 #3
Hi,
Thanks.
I did try using the code sample from codeguru. I need to repeatedly
search all subdirectories under the Start Menu and display the programs
the current user can view/execute. Mainly, I also need to display the
result as file names or description , rather than the full paths which
these code samples result in.

Jan 24 '06 #4
Thanks .
The code at
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=getfiledescription&lang=en>
, when executed , shows only the type name as and not the file name.
For e.g., if file retrieved is a shortcut to the McAfee application,
then it should display as "McAfee SpamKiller" and not as "C:\Documents
and Settings\User\Start Menu\Programs\McAfee".
Is there a way out? I would appreciate any help.

Jan 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrea.gavana | last post by:
Hello NG, I have searched everyweher, and I am not able to find a solution... basically, I am constructing a GUI with wxPython, in which I have a list. In this list control, I have some file. I...
7
by: Alexander Keßler | last post by:
Hi, I asked u this morning: --------------------------------------- I want to programm some kind of Explorer like the Windows Explorer. And I want to know how can i get the same Icons/Symbols...
7
by: Shane Story | last post by:
I want to be able to get the icon associated with a certain file extension. I can read the registry hkey_classes to get the ext. then look to the other key for default icon. How can I then...
3
by: Brian Henry | last post by:
How would you get the associated icon for the file type of a file with a certain extension? like BMP, JPEG, MPEG, etc etc find their default icon using the File Types listing that associates the...
4
by: Giovanni pepe | last post by:
I have download from site Microsoft the procedure for Get Associated Icon from File Article is : "How to use the SHGetFileInfo function to get the icons that are associated with files in Visual...
5
by: IcingDeath via DotNetMonster.com | last post by:
I am building this SQL Server database app in which i can store files. In order to display files I want to have the app show the associated icon for the extension of the file that is in the...
1
by: Neo | last post by:
please tell me, how get icon structure associated with extension? if my file extention is (Doc) then how and where i get icon of document file? regards, Mohammad Omer Nasir
1
by: gevald123 | last post by:
I'm storing system files in a sql server database, as "image" datatype fields. In my front end (vb .net 1.1) I'd like to display these files in a listview with the "Windows Explorer" feel and...
5
by: =?Utf-8?B?TG9hbldvbGY=?= | last post by:
I'm working on a Windows Explorer-like application. What is the best way to find out what application is set to open a particular type of file? Is there a function to do this? Do I have to go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.