begin process at 2012 02 13 20:56:46
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Fichier / Disque

 > RETROUVER L'ICONE DE L'EXPLORER ASSOCIÉ À UN FICHIER

RETROUVER L'ICONE DE L'EXPLORER ASSOCIÉ À UN FICHIER


 Information sur la source

Note :
8 / 10 - par 1 personne
8,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Fichier / Disque Source .NET ( DotNet ) Niveau :Débutant Date de création :19/11/2004 Date de mise à jour :19/11/2004 11:37:27 Vu :7 839

Auteur : melkor18

Ecrire un message privé
Commentaire sur cette source (13)
Ajouter un commentaire et/ou une note

 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

Source avec Zip Source avec une capture Source .NET (Dotnet) MKV EXTRACTEUR

 Sources de la même categorie

Source avec Zip Source .NET (Dotnet) MODIFIER LES EXTENSION DES FICHIERS par okosa
ROUTINE DIR RÉCURSIVE POUR OBTENIR LA LISTE DE TOUS LES FICH... par kerisolde
Source avec Zip Source avec une capture FILE,SECURITY,FICHIER par okosa
Source avec Zip Source avec une capture Source .NET (Dotnet) PATCHEUR DE FICHIER par tototh
Source avec Zip Source avec une capture LECTURE DES INFORMATIONS DES DISQUES COMPOSANT UN ENSEMBLE R... par jack

Commentaires et avis

Commentaire de spy166 le 19/11/2004 13:16:04

Hmmm du réchauffé !

Commentaire de Alain Proviste le 21/11/2004 01:04:06 administrateur CS

C'est effectivement du réchauffé mais il n'en reste pas moins que c'était à faire en vb.net
Les marshall c'est lourd et c'est pas plus mal quand qq d'autre s'est dejà pris la tête dessus.
Merci pour cette participation :p

Commentaire de spy166 le 21/11/2004 09:56:37

Ce que je veux dire c'est que ça a été déjà fait.
Je dirais même que là j'ai l'impression que c'est du copier-coller.

Commentaire de julien__ le 23/03/2005 10:39:41

J'ai un petit problème avec ce code... Ca marche avec tous les types de fichiers, sauf les .exe?

Commentaire de Alain Proviste le 23/03/2005 14:24:03 administrateur CS

c'est du .net on ne peux pas l'ouvrir avec vb6.

Commentaire de Alain Proviste le 23/03/2005 14:24:09 administrateur CS

c'est du .net on ne peux pas l'ouvrir avec vb6.

Commentaire de RogerAirFoil le 15/06/2006 21:34:14

Modification pour le bug .exe

ligne 76 à 92

    Public ReadOnly Property AssociatedIcon(ByVal IconSize As IconType, Optional ByVal MustExist As Boolean = True) As Icon
        Get
            If MustExist Then
                If Me.Exists Then
  supprimer (inutile)   'Dim Info As New FileInfo(Me.OriginalPath)
  supprimer             'AssociatedIcon = GetIcon(Info.Extension, IconSize)
  ajouter              AssociatedIcon = GetIcon(Me.OriginalPath, IconSize)
  supprimer (inutile)   'Info = Nothing
                Else
                    AssociatedIcon = Nothing
                End If
            Else
supprimer (inutile)   'Dim Info As New FileInfo(Me.OriginalPath)
  supprimer             'AssociatedIcon = GetIcon(Info.Extension, IconSize)
  ajouter              AssociatedIcon = GetIcon(Me.OriginalPath, IconSize)
  supprimer (inutile)   'Info = Nothing
            End If
        End Get
    End Property

Commentaire de julien__ le 20/09/2006 01:35:52

Cette classe est d'une grande utilité merci de l'avoir postée. Au fait, je cherche à faire la même chose, mais sans avoir besoin de donner un fichier en référence. Je cherche à faire un fonction me retorunant l'icone associé à une extension.
Pare ex. une fonction à laquelle je donne seulement l'extension sous forme d'une string ".txt" et me retorunant l'icone associé.. quelqu'un à une idée?

Commentaire de M51 le 12/04/2007 17:04:07

Pour info Julien voir le lien:
http://www.vbfrance.com/code.aspx?ID=41631

Commentaire de dockyf le 14/05/2007 11:49:24

C'est bien le type de source que je recherche, mais je débute et je ne parviens pas à utiliser la classe.
Un peu d'aide serait le bienvenue.

Merci par avance.

FAB

Commentaire de magicgus le 21/03/2010 14:32:41

Salut, pareil que dockyf,

Dans ton programme ça marche mais étrangement quand je l'implémente dans un de mes projets, il me charge l'icone (avec la flèche pour les raccourcis) et non la miniature... Moi y'a n'a pas conprendre...

Commentaire de magicgus le 21/03/2010 14:33:34

oups désolé je me suis trompé d'onglet dans fofox ;)

Commentaire de blq le 11/12/2010 23:26:35

Je suis pas un expert, mais n'est-il pas plus simple de faire :
thePictureBox.Image = System.Drawing.Icon.ExtractAssociatedIcon(theFile).ToBitmap()

Il y a peut-être des limites, mais cela fonctionne, du moins avec VB 2010 Express.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 14,820 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales