Accueil > > > RETROUVER L'ICONE DE L'EXPLORER ASSOCIÉ À UN FICHIER
RETROUVER L'ICONE DE L'EXPLORER ASSOCIÉ À UN FICHIER
Information sur la source
Description
C'est une classe qui dérive de la classe System.IO.FileSystemInfo, à laquelle j'ai ajouté les bénéfices d'une petite API qui me permet de retourver l'icone associé au fichier et qu'il doit s'afficher dans l'explorer.
Source
- Imports System.IO
- Imports Microsoft.Win32
- Imports System
- Imports System.Drawing
- Imports System.Runtime.InteropServices
-
- Public Class AdvancedFileSysInfo
-
- Inherits System.IO.FileSystemInfo
-
- ' Enumération des valeurs dispo pour le paramètre flag de SHGetFileInfo
- <Flags()> Private Enum SHGFI
- SmallIcon = &H1
- LargeIcon = &H0
- Icon = &H100
- DisplayName = &H200
- Typename = &H400
- SysIconIndex = &H4000
- UseFileAttributes = &H10
- End Enum
-
- Public Enum IconType
- SmallIcon = True
- LargeIcon = False
- End Enum
-
- ' Structure contenant les informations sur un objet du filesystem
- <StructLayout(LayoutKind.Sequential)> _
- Private Structure SHFILEINFO
- Public hIcon As IntPtr
- Public iIcon As Integer
- Public dwAttributes As Integer
- <MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> _
- Public szDisplayName As String
- <MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> _
- Public szTypeName As String
-
- Public Sub New(ByVal B As Boolean)
- hIcon = IntPtr.Zero
- iIcon = 0
- dwAttributes = 0
- szDisplayName = vbNullString
- szTypeName = vbNullString
- End Sub
- End Structure
-
- ' Permet de retourver les informations sur un fichier, un répertoire, un disque
- Private Declare Auto Function SHGetFileInfo Lib "shell32" ( _
- ByVal pszPath As String, ByVal dwFileAttributes As Integer, _
- ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlagsn As SHGFI) As Integer
-
-
- Public Overrides ReadOnly Property Name() As String
- Get
- Dim Info As New FileInfo(Me.OriginalPath)
- Name = Info.Name
- Info = Nothing
- End Get
- End Property
-
- Public Overrides Sub Delete()
- Dim Info As New FileInfo(Me.OriginalPath)
- Info.Delete()
- Info = Nothing
- End Sub
-
- Public Overrides ReadOnly Property Exists() As Boolean
- Get
- Dim Info As New FileInfo(Me.OriginalPath)
- Exists = Info.Exists
- Info = Nothing
- End Get
- End Property
-
- ' Cette propriété retourne un icone, celui affiché dans l'explorer
- Public ReadOnly Property AssociatedIcon(ByVal IconSize As IconType, Optional ByVal MustExist As Boolean = True) As Icon
- Get
- If MustExist Then
- If Me.Exists Then
- Dim Info As New FileInfo(Me.OriginalPath)
- AssociatedIcon = GetIcon(Info.Extension, IconSize)
- Info = Nothing
- Else
- AssociatedIcon = Nothing
- End If
- Else
- Dim Info As New FileInfo(Me.OriginalPath)
- AssociatedIcon = GetIcon(Info.Extension, IconSize)
- Info = Nothing
- End If
- End Get
- End Property
-
- ' Permet de récupérer l'icone du fichier tel qu'il apparait dans l'explorer
- Private Function GetIcon(ByVal Path As String, Optional ByVal Ico As IconType = True) As Icon
- Dim info As New SHFILEINFO(True)
- Dim cbSizeInfo As Integer = Marshal.SizeOf(info)
- Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAttributes
- If Ico = True Then
- flags += SHGFI.SmallIcon
- Else
- flags += SHGFI.LargeIcon
- End If
- SHGetFileInfo(Path, 256, info, cbSizeInfo, flags)
- Return Icon.FromHandle(info.hIcon)
- End Function
-
- Private Function ExtractDefaultIcon() As String
-
-
- Dim HKROOT As Registry
- Dim hsubKey As RegistryKey
- Dim sApplication As String
-
- ExtractDefaultIcon = ""
-
- hsubKey = HKROOT.ClassesRoot.OpenSubKey(Me.Extension)
-
- If Not hsubKey Is Nothing Then
-
- sApplication = hsubKey.GetValue("")
- hsubKey.Close()
- hsubKey = HKROOT.ClassesRoot.OpenSubKey(sApplication & "\DefaultIcon")
- If Not hsubKey Is Nothing Then
- ExtractDefaultIcon = hsubKey.GetValue("")
- hsubKey.Close()
- End If
- End If
-
- hsubKey = Nothing
- HKROOT = Nothing
- End Function
-
- Public Sub New(ByVal FileName As String)
- Me.OriginalPath = FileName
- End Sub
-
- End Class
Imports System.IO
Imports Microsoft.Win32
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Public Class AdvancedFileSysInfo
Inherits System.IO.FileSystemInfo
' Enumération des valeurs dispo pour le paramètre flag de SHGetFileInfo
<Flags()> Private Enum SHGFI
SmallIcon = &H1
LargeIcon = &H0
Icon = &H100
DisplayName = &H200
Typename = &H400
SysIconIndex = &H4000
UseFileAttributes = &H10
End Enum
Public Enum IconType
SmallIcon = True
LargeIcon = False
End Enum
' Structure contenant les informations sur un objet du filesystem
<StructLayout(LayoutKind.Sequential)> _
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> _
Public szTypeName As String
Public Sub New(ByVal B As Boolean)
hIcon = IntPtr.Zero
iIcon = 0
dwAttributes = 0
szDisplayName = vbNullString
szTypeName = vbNullString
End Sub
End Structure
' Permet de retourver les informations sur un fichier, un répertoire, un disque
Private Declare Auto Function SHGetFileInfo Lib "shell32" ( _
ByVal pszPath As String, ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlagsn As SHGFI) As Integer
Public Overrides ReadOnly Property Name() As String
Get
Dim Info As New FileInfo(Me.OriginalPath)
Name = Info.Name
Info = Nothing
End Get
End Property
Public Overrides Sub Delete()
Dim Info As New FileInfo(Me.OriginalPath)
Info.Delete()
Info = Nothing
End Sub
Public Overrides ReadOnly Property Exists() As Boolean
Get
Dim Info As New FileInfo(Me.OriginalPath)
Exists = Info.Exists
Info = Nothing
End Get
End Property
' Cette propriété retourne un icone, celui affiché dans l'explorer
Public ReadOnly Property AssociatedIcon(ByVal IconSize As IconType, Optional ByVal MustExist As Boolean = True) As Icon
Get
If MustExist Then
If Me.Exists Then
Dim Info As New FileInfo(Me.OriginalPath)
AssociatedIcon = GetIcon(Info.Extension, IconSize)
Info = Nothing
Else
AssociatedIcon = Nothing
End If
Else
Dim Info As New FileInfo(Me.OriginalPath)
AssociatedIcon = GetIcon(Info.Extension, IconSize)
Info = Nothing
End If
End Get
End Property
' Permet de récupérer l'icone du fichier tel qu'il apparait dans l'explorer
Private Function GetIcon(ByVal Path As String, Optional ByVal Ico As IconType = True) As Icon
Dim info As New SHFILEINFO(True)
Dim cbSizeInfo As Integer = Marshal.SizeOf(info)
Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAttributes
If Ico = True Then
flags += SHGFI.SmallIcon
Else
flags += SHGFI.LargeIcon
End If
SHGetFileInfo(Path, 256, info, cbSizeInfo, flags)
Return Icon.FromHandle(info.hIcon)
End Function
Private Function ExtractDefaultIcon() As String
Dim HKROOT As Registry
Dim hsubKey As RegistryKey
Dim sApplication As String
ExtractDefaultIcon = ""
hsubKey = HKROOT.ClassesRoot.OpenSubKey(Me.Extension)
If Not hsubKey Is Nothing Then
sApplication = hsubKey.GetValue("")
hsubKey.Close()
hsubKey = HKROOT.ClassesRoot.OpenSubKey(sApplication & "\DefaultIcon")
If Not hsubKey Is Nothing Then
ExtractDefaultIcon = hsubKey.GetValue("")
hsubKey.Close()
End If
End If
hsubKey = Nothing
HKROOT = Nothing
End Function
Public Sub New(ByVal FileName As String)
Me.OriginalPath = FileName
End Sub
End Class
Historique
- 19 novembre 2004 11:37:27 :
- ...
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|