Obtenir : la taille d'un fichier | le programme associé à un type de fichier | un répertoire spécial


Propriétés du code


Date de création : 02/01/2007 à 00:05:00
6 Commentaire(s)
  violent_ken

 

Présentation


Trois fonctions permettant d'obtenir :
-la taille d'un fichier (pas de limitation de taille contrairement à FileLen)
-le path de l'executable associé à un type de fichier
-un répertoire spécial (documents, windows, program files...etc.)

 

Code


Option Explicit


'-------------------------------------------------------
'CONSTANTES
'-------------------------------------------------------
Private Const GENERIC_READ                  As Long = &H80000000
Private Const FILE_SHARE_READ              As Long = &H1
Private Const FILE_SHARE_WRITE              As Long = &H2
Private Const OPEN_EXISTING                As Long = 3

   

'-------------------------------------------------------
'TYPES & ENUMS
'-------------------------------------------------------
Public Enum SPECIAL_FOLDER_TYPE
    CSIDL_DESKTOP = &H0                '{desktop}
    CSIDL_INTERNET = &H1                'Internet Explorer (icon on desktop)
    CSIDL_PROGRAMS = &H2                'Start Menu\Programs
    CSIDL_CONTROLS = &H3                'My Computer\Control Panel
    CSIDL_PRINTERS = &H4                'My Computer\Printers
    CSIDL_PERSONAL = &H5                'My Documents
    CSIDL_FAVORITES = &H6              '{user}\Favourites
    CSIDL_STARTUP = &H7                'Start Menu\Programs\Startup
    CSIDL_RECENT = &H8                  '{user}\Recent
    CSIDL_SENDTO = &H9                  '{user}\SendTo
    CSIDL_BITBUCKET = &HA              '{desktop}\Recycle Bin
    CSIDL_STARTMENU = &HB              '{user}\Start Menu
    CSIDL_DESKTOPDIRECTORY = &H10      '{user}\Desktop
    CSIDL_DRIVES = &H11                'My Computer
    CSIDL_NETWORK = &H12                'Network Neighbourhood
    CSIDL_NETHOOD = &H13                '{user}\nethood
    CSIDL_FONTS = &H14                  'windows\fonts
    CSIDL_TEMPLATES = &H15
    CSIDL_COMMON_STARTMENU = &H16      'All Users\Start Menu
    CSIDL_COMMON_PROGRAMS = &H17        'All Users\Programs
    CSIDL_COMMON_STARTUP = &H18        'All Users\Startup
    CSIDL_COMMON_DESKTOPDIRECTORY = &H19 'All Users\Desktop
    CSIDL_APPDATA = &H1A                '{user}\Application Data
    CSIDL_PRINTHOOD = &H1B              '{user}\PrintHood
    CSIDL_LOCAL_APPDATA = &H1C          '{user}\Local Settings\Application Data (non roaming)
    CSIDL_ALTSTARTUP = &H1D            'non localized startup
    CSIDL_COMMON_ALTSTARTUP = &H1E      'non localized common startup
    CSIDL_COMMON_FAVORITES = &H1F
    CSIDL_INTERNET_CACHE = &H20
    CSIDL_COOKIES = &H21
    CSIDL_HISTORY = &H22
    CSIDL_COMMON_APPDATA = &H23          'All Users\Application Data
    CSIDL_WINDOWS = &H24                'GetWindowsDirectory()
    CSIDL_SYSTEM = &H25                  'GetSystemDirectory()
    CSIDL_PROGRAM_FILES = &H26          'C:\Program Files
    CSIDL_MYPICTURES = &H27              'C:\Program Files\My Pictures
    CSIDL_PROFILE = &H28                'USERPROFILE
    CSIDL_SYSTEMX86 = &H29              'x86 system directory on RISC
    CSIDL_PROGRAM_FILESX86 = &H2A        'x86 C:\Program Files on RISC
    CSIDL_PROGRAM_FILES_COMMON = &H2B    'C:\Program Files\Common
    CSIDL_PROGRAM_FILES_COMMONX86 = &H2C 'x86 Program Files\Common on RISC
    CSIDL_COMMON_TEMPLATES = &H2D        'All Users\Templates
    CSIDL_COMMON_DOCUMENTS = &H2E        'All Users\Documents
    CSIDL_COMMON_ADMINTOOLS = &H2F      'All Users\Start Menu\Programs\Administrative Tools
    CSIDL_ADMINTOOLS = &H30              '{user}\Start Menu\Programs\Administrative Tools
End Enum
Private Type SHITEMID
    cb As Long
    abID As Byte
End Type
Private Type ITEMIDLIST
    mkid As SHITEMID
End Type



'-------------------------------------------------------
'APIS
'-------------------------------------------------------
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, lpFileSize As Currency) As Boolean
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long


'-------------------------------------------------------
'obtient le programme associé à un fichier
'-------------------------------------------------------
Public Function GetAssociatedExecutableProgram(ByVal sFile As String) As String
Dim sBuf As String

    sBuf = String$(255, 0)  'création d'un buffer
   
    'trouve l'éxécutable associé
    FindExecutable sFile, vbNullString, sBuf
   
    'formate la string
    GetAssociatedExecutableProgram = Left$(sBuf, InStr(sBuf, vbNullChar) - 1)

End Function

'-------------------------------------------------------
'obtient la taille d'un fichier, même si celle ci est supérieure à 4Go (long)
'-------------------------------------------------------
Public Function GetFileSize(ByVal sFile As String) As Currency
Dim lngFile As Long
Dim curSize As Currency

    'obtient le handle du fichier
    lngFile = CreateFile(sFile, GENERIC_READ, FILE_SHARE_WRITE Or FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
   
    'obtient la taille par API
    GetFileSizeEx lngFile, curSize
   
    'ferme le handle ouvert
    CloseHandle lngFile
   
    GetFileSize = curSize * 10000 'multiplie par 10^4 pour obtenir un nombre entier
End Function

'-------------------------------------------------------
'obtient un path spécial
'-------------------------------------------------------
Public Function GetSpecialFolder(Folder As SPECIAL_FOLDER_TYPE, Optional ByVal hwnd As Long) As String
Dim ret As Long
Dim sPath As String
Dim IDL As ITEMIDLIST

    'obtient le path
    ret = SHGetSpecialFolderLocation(hwnd, Folder, IDL)
   
    If ret = 0 Then
        'création d'un buffer
        sPath = Space$(512) '512 > MAX_PATH
        'obtient le path depuis l'itemidlist
        ret = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
        'formate le texte
        GetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1)
        Exit Function
    End If
   
    GetSpecialFolder = vbNullString
End Function


 
 

Modifier le code

Seul les admins et l'auteur du code lui même peuvent modifier ce code.

 

Commentaires


De sebdraluorg le 02/01/2007 à 18:09


Plop,

yep c'est cool j'avais pas trouvé les CSIDL_...X86
T'as trouvé ca ou ?

 

De violent_ken le 02/01/2007 à 18:47


lu ;)

Hum, je me rappelais plus (ce code n'est pas tout tout récent), et a priori, et après une recherche google, j'ai récupéré les constantes ici: http://vbnet.mvps.org/index.html?code/browse/shpathidlist.htm (il y a les mêmes commentaires en anglais)

 

De MadMatt le 07/02/2007 à 15:13


salut,
chui en train d'ajouter le code à la librairie, par contre y'a un truc que j'ai pas pigé, c'est l'utilité du hWnd ?
Il faut passer le handle d'une fenetre pour l'appel d'api ? On peut pas s'en passer en choisissant un handle au hasard ? Et j'ai retrouvé un vieux code dans le meme genre, en mettant 0 ça avait l'air de marcher
@+

 

De violent_ken le 07/02/2007 à 19:27


Oui, le hWnd ne m'a pas paru très utile (en tout cas çà change rien si on met 1 ou celui d'un fenêtre), c'est pour çà que je l'ai mis que en Optional (parce qu'il est quand même nécessaire à l'API).

 

De violent_ken le 07/02/2007 à 19:29


Après un rapide tour sur MSDN, il semblerait que dans notre cas cet argument soit inutile.
@+

 

De MadMatt le 07/02/2007 à 19:50


Ok merci pour les infos si rapidement, bon bah j'ai mis 0 à la place ça simplifie la fonction.

 

Ajouter un commentaire


Vous devez être connecté pour pouvoir poster un commentaire.

 
 

Valid HTML 4.01 Transitional Valid CSS

Site web de Vb System Library version 1.3
Developpement et design réalisé par : Matthieu Napoli (MadMatt)
© 2007 Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
Temps d'execution de la page : 0.013 s
www.mnapoli.fr