2010-01-18 7 views
4

Comment récupérer les icônes des fichiers associés aux types de fichiers et les ajouter avec les articles de Listview dans vb.neticônes Fichier et liste Voir

Je l'ai lu SHGetFileInfo mais je ne comprenais rien de ce

s'il vous plaît me donner une solution ou s'il vous plaît me expliquer le système ho fonctionne avec les contrôles .NET dans les détails

+0

Possibilité de duplication de [Utiliser l'image des types de fichiers du système d'exploitation en C# .NET] (http://stackoverflow.com/questions/5164794/use-the-file-types-image-of-the-operating- système en c-net/5164875) –

Répondre

6

Après avoir Leva les yeux SHGetFileInfo Je comprends pourquoi vous êtes confus par elle, mais je pense qu'il pourrait être un peu exagéré pour ce que je pense vous essayez de faire, c'est-à-dire d'énumérer le contenu d'un dossier et d'ajouter des éléments à la liste. Si nous avons un formulaire qui contient un ListView et une ImageList, avec les deux liés en ayant la propriété LargeImageList ListView définie sur ImageList, voici comment nous mettons le contenu d'un dossier dans le ListView avec les icônes provenant de le fichier EXE associé pour chaque fichier.

Imports System.IO 
Imports System.Drawing 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    Dim dirInfo As DirectoryInfo 
    Dim fileInfo As FileInfo 
    Dim exePath As String 
    Dim exeIcon As Icon 

    dirInfo = New DirectoryInfo(path_to_some_folder 

    'We use this For...Each to iterate over the collection of files in the folder 
    For Each fileInfo In dirInfo.GetFiles 
     'We can only find associated exes by extension, so don't show any files that have no extension 
     If fileInfo.Extension = String.Empty Then 
     Else 
      'Use the function to get the path to the executable for the file 
      exePath = GetAssociatedProgram(fileInfo.Extension) 

      'Use ExtractAssociatedIcon to get an icon from the path 
      exeIcon = Drawing.Icon.ExtractAssociatedIcon(exePath) 

      'Add the icon if we haven't got it already, with the executable path as the key 
      If ImageList1.Images.ContainsKey(exePath) Then 
      Else 
       ImageList1.Images.Add(exePath, exeIcon) 
      End If 

      'Add the file to the ListView, with the executable path as the key to the ImageList's image 
      ListView1.Items.Add(fileInfo.Name, exePath) 
     End If 
    Next 

End Sub 

GetAssociatedProgram vient de developer.com

Public Function GetAssociatedProgram(ByVal FileExtension As _ 
    String) As String 


    ' Returns the application associated with the specified 
    ' FileExtension 
    ' ie, path\denenv.exe for "VB" files 
    Dim objExtReg As Microsoft.Win32.RegistryKey = _ 
     Microsoft.Win32.Registry.ClassesRoot 
    Dim objAppReg As Microsoft.Win32.RegistryKey = _ 
     Microsoft.Win32.Registry.ClassesRoot 
    Dim strExtValue As String 
    Try 
     ' Add trailing period if doesn't exist 
     If FileExtension.Substring(0, 1) <> "." Then _ 
      FileExtension = "." & FileExtension 
     ' Open registry areas containing launching app details 
     objExtReg = objExtReg.OpenSubKey(FileExtension.Trim) 
     strExtValue = objExtReg.GetValue("").ToString 
     objAppReg = objAppReg.OpenSubKey(strExtValue & _ 
         "\shell\open\command") 
     ' Parse out, tidy up and return result 
     Dim SplitArray() As String 
     SplitArray = Split(objAppReg.GetValue(Nothing).ToString, """") 
     If SplitArray(0).Trim.Length > 0 Then 
      Return SplitArray(0).Replace("%1", "") 
     Else 
      Return SplitArray(1).Replace("%1", "") 
     End If 
    Catch 
     Return "" 
    End Try 
End Function 

A la fin de tout cela, lorsque vous exécutez ce code sur ce dossier: alt text http://www.philippursglove.com/stackoverflow/listview1.png vous devriez obtenir:
alt text http://www.philippursglove.com/stackoverflow/listview2.png

+0

exeIcon = Drawing.Icon.ExtractAssociatedIcon (ExePath) pour cette déclaration me envoie son exception Le chemin n'est pas la forme Legel – shraddha

+0

je peux obtenir cette exception si l'un des fichiers dans le dossier n'a pas d'extension. Dans ce cas, vous devez soit exclure les fichiers sans extension (j'ai édité ma réponse pour l'afficher), soit ajouter une icône de fichier vide à ImageList, puis utiliser cette icône pour tous les fichiers sans extension. – PhilPursglove

-1

Si vous connaissez le nom de fichier du fichier dont vous voulez l'icône, vous pouvez utiliser la fonction System.Drawing.Icon.ExtractAssociatedIcon à cet effet. par exemple.

Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe"); 

Vous pouvez également consulter ma réponse à une related question pour plus de détails de mise en œuvre.