IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

[VB.NET] Listing d'un répertoire par FTP avec API


Sujet :

VB.NET

  1. #1
    Membre habitué Avatar de joefou
    Profil pro
    Inscrit en
    Février 2005
    Messages
    248
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 248
    Points : 177
    Points
    177
    Par défaut [VB.NET] Listing d'un répertoire par FTP avec API
    [VS.NET 2003]

    Salut à tous !

    J'ai un soucis dans la récupération du listing d'un répertoire Unix via les API gérant le FTP. Voici ce que j'utilise, c'est un code issu de VB5 que j'ai réadapté:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
     
    Public Class FTPManagerAPI
     
        Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Integer) As Integer
        Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer
        Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer
        Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Integer, ByVal lpszDirectory As String) As Boolean
        Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Integer, ByVal lpszCurrentDirectory As String, ByVal lpdwCurrentDirectory As Integer) As Integer
        Public Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Integer, ByVal lpszDirectory As String) As Boolean
        Public Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Integer, ByVal lpszDirectory As String) As Boolean
        Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Integer, ByVal lpszFileName As String) As Boolean
        Public Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Integer, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
        Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByRef dwContext As Integer) As Boolean
        Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Integer, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
        Public Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (ByVal lpdwError As Integer, ByVal lpszBuffer As String, ByVal lpdwBufferLength As Integer) As Boolean
        Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Integer, ByVal lpszSearchFile As String, ByVal lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Integer, ByVal dwContent As Integer) As Integer
        Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Integer, ByVal lpvFindData As WIN32_FIND_DATA) As Integer
        Public Declare Function LocalFileTimeToFileTime Lib "kernel32" (ByVal lpLocalFileTime As FILETIME, ByVal lpFileTime As FILETIME) As Integer
     
     
        Public Enum eTRANSFERT_TYPE
            FTP_TRANSFER_TYPE_ASCII = &H1
            FTP_TRANSFER_TYPE_BINARY = &H2
            FTP_TRANSFER_TYPE_UNKNOWN = &H0
        End Enum
     
        Public Const INTERNET_DEFAULT_FTP_PORT As Short = 21               ' default for FTP servers
        Public Const INTERNET_SERVICE_FTP As Integer = 1
        Public Const INTERNET_FLAG_PASSIVE As Integer = &H8000000            ' used for FTP connections
        Public Const INTERNET_OPEN_TYPE_PRECONFIG As Integer = 0                    ' use registry configuration
        Public Const INTERNET_OPEN_TYPE_DIRECT As Integer = 1                         ' direct to net
        Public Const INTERNET_OPEN_TYPE_PROXY As Integer = 3                         ' via named proxy
        Public Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY As Integer = 4   ' prevent using java/script/INS
        Public Const MAX_PATH As Integer = 260
        Public Const PassiveConnection As Boolean = True
     
        Public Structure FILETIME
            Public dwLowDateTime As Integer
            Public dwHighDateTime As Integer
        End Structure
     
        Public Structure SYSTEMTIME
            Public wYear As Integer
            Public wMonth As Integer
            Public wDayOfWeek As Integer
            Public wDay As Integer
            Public wHour As Integer
            Public wMinute As Integer
            Public wSecond As Integer
            Public wMilliseconds As Integer
        End Structure
     
        Public Structure WIN32_FIND_DATA
            Public dwFileAttributes As Integer
            Public ftCreationTime As FILETIME
            Public ftLastAccessTime As FILETIME
            Public ftLastWriteTime As FILETIME
            Public nFileSizeHigh As Integer
            Public nFileSizeLow As Integer
            Public dwReserved0 As Integer
            Public dwReserved1 As Integer
            <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=260)> Public cFileName As String
            <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=14)> Public cAlternate As String
        End Structure
     
        Private m_ListeFichiers As Collection
     
        '------------------------------
        'Constructeur
        '------------------------------
        Public Sub New()
            m_ListeFichiers = New Collection
        End Sub
     
        '------------------------------------------------
        'Completer une chaine de caracteres par la droite
        ' Texte: chaine à compléter
        ' Taille: taille que la chaine doit atteindre
        ' Caractere: caractere de completion
        ' Retour: chaine complétée à droite
        '------------------------------------------------
        Private Function CompleterDroite(ByVal Texte As String, ByVal Taille As Integer, Optional ByVal Caractere As String = " ")
            Dim I As Integer
            Dim TexteRetour As String
            TexteRetour = Texte
            TexteRetour = TexteRetour.Trim()
            If TexteRetour.Equals(String.Empty) Then
                For I = 0 To Taille - 1
                    TexteRetour = TexteRetour & Caractere
                Next I
            ElseIf TexteRetour.Length > Taille Then
                TexteRetour = TexteRetour.Substring(0, Taille)
            Else
                For I = TexteRetour.Length To Taille - 1
                    TexteRetour = TexteRetour & Caractere
                Next I
            End If
            Return TexteRetour
        End Function
     
        '---------------------------------
        'Lister les fichiers
        '---------------------------------
        Public Function EnumFiles(ByVal stServ As String, ByVal stLogin As String, ByVal stPass As String, ByVal stRepFtp As String, ByVal stFicFtp As String)
     
            Dim pData As New WIN32_FIND_DATA
            Dim hFind As Integer
            Dim lRet As Integer
     
            Dim lgFtp As Integer
            Dim lgSession, v As Integer
     
     
            lgSession = InternetOpen("EnumFiles", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
     
            If lgSession Then
     
                lgFtp = InternetConnect(lgSession, stServ, INTERNET_DEFAULT_FTP_PORT, stLogin, stPass, INTERNET_SERVICE_FTP, 0, 0)
     
                If lgFtp Then
     
                    If FtpSetCurrentDirectory(lgFtp, stRepFtp) Then
     
                        pData.cFileName = CompleterDroite("", MAX_PATH, Chr(0))
     
                        hFind = FtpFindFirstFile(lgFtp, stFicFtp, pData, 0, 0)
     
                        If hFind = 0 Then Exit Function
     
                        m_ListeFichiers.Add(EpurFic(Left(pData.cFileName, InStr(1, pData.cFileName, Chr(0), vbBinaryCompare) - 1)))
     
                        Do
     
                            pData.cFileName = CompleterDroite("", MAX_PATH, Chr(0))
     
                            lRet = InternetFindNextFile(hFind, pData)   '<<<<<<<< LIGNE QUI POSE PROBLEME
     
                            If lRet = 0 Then Exit Do
     
                            v = pData.ftLastWriteTime.dwHighDateTime
     
                            m_ListeFichiers.Add(EpurFic(Left(pData.cFileName, InStr(1, pData.cFileName, Chr(0), vbBinaryCompare) - 1)))
                        Loop
     
                        InternetCloseHandle(hFind)
     
     
                    Else
                        MsgBox("Erreur de positionnement dans le répertoire distant!")
                    End If
     
     
                Else
                    MsgBox("Erreur ouverture session FTP, vérifiez l'adresse, le login/pwd.")
                End If
     
            Else
                MsgBox("Pas de connexion!")
            End If
     
     
        End Function
     
    .....
    Quand je parviens à la ligne "lRet = InternetFindNextFile(hFind, pData)" de la méthode "EnumFiles", j'ai toujours un résultat à 0 (les chemins unix sont corrects, et les répertoires que je tente de lister possèdent toujours des fichiers)

    Quelqu'un a-t'il une idée ?
    D'avance merci !
    "On peut dire qu’il est possible de savoir comment vaincre sans être capable de le faire" - Sun Tze, in L'art de la guerre

    "Mais t'es complètement naze ou quoi ?! UNE pomme ! ça fait DEUX moitiés de pomme !! mais quand vas-tu le comprendre ?! J'en ai ma claque, j'me tire !" - Bouddha et un disciple, dans un moment de faiblesse

  2. #2
    Membre habitué Avatar de joefou
    Profil pro
    Inscrit en
    Février 2005
    Messages
    248
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 248
    Points : 177
    Points
    177
    Par défaut
    Ayé ! J'ai trouvé !
    Il s'agissait des déclarations des fonctions FtpFindFirstFile et InternetFindNextFile qui posaient problème ! à cause d'un "Auto" manquant dans le Declare...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    public Declare Auto Function FtpFindFirstFile _
                            Lib "wininet.dll" Alias "FtpFindFirstFileA" _
                            (ByVal hFtpSession As Integer, _
                            ByVal lpszSearchFile As String, _
                            ByRef lpFindFileData As WIN32_FIND_DATA, _
                            ByVal dwFlags As Integer, _
                            ByVal dwContent As Integer) As Integer
        Public Declare Auto Function InternetFindNextFile _
                            Lib "wininet.dll" Alias "InternetFindNextFileA" _
                            (ByVal hFind As Integer, _
                            ByRef lpvFindData As WIN32_FIND_DATA) As Integer
    "On peut dire qu’il est possible de savoir comment vaincre sans être capable de le faire" - Sun Tze, in L'art de la guerre

    "Mais t'es complètement naze ou quoi ?! UNE pomme ! ça fait DEUX moitiés de pomme !! mais quand vas-tu le comprendre ?! J'en ai ma claque, j'me tire !" - Bouddha et un disciple, dans un moment de faiblesse

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Récuperer un fichier par FTP avec PDI
    Par Archi44 dans le forum kettle/PDI
    Réponses: 4
    Dernier message: 30/09/2013, 14h59
  2. Destruction d'un répertoire par FTP
    Par laurentSc dans le forum Débuter
    Réponses: 1
    Dernier message: 14/01/2010, 11h48
  3. Transférer des assemblies par FTP avec Maven 2
    Par romaintaz dans le forum Maven
    Réponses: 4
    Dernier message: 19/10/2009, 13h37
  4. Copie de fichier par FTP avec un BAT
    Par Thony_7 dans le forum Windows
    Réponses: 11
    Dernier message: 31/05/2007, 16h54
  5. Upload par FTP avec des gros fichiers
    Par __fabrice dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 8
    Dernier message: 07/06/2006, 12h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo