Hello all,
I am having some trouble using wininet.dll fot FTP in VB.NET. Here is my code:
The problem is that FtpSetCurrentDirectory doesn't work (returns False always) and I know that the remote directory exists (I can navigate to it using WS_FTP). Also, FtpGetCurrentDirectory returns True, but the FileList string is empty, the current path is not returned. The FtpGetFile function works just fine, strangely. The FtpFindFirstFile function also returns true, but when I check the values of the FindData structure, it is empty.
Any ideas? This is driving me nuts!
Thanks,
JEB
I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
I am having some trouble using wininet.dll fot FTP in VB.NET. Here is my code:
Code:
Module Module1
Private Structure FILETIME
Dim dwLowDateTime As Long
Dim dwHighDateTime As Long
End Structure
Private Structure WIN32_FIND_DATA
Dim dwFileAttributes As Long
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Long
Dim nFileSizeLow As Long
Dim dwReserved0 As Long
Dim dwReserved1 As Long
Dim cFileName As String
Dim cAlternate As String
End Structure
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer
Private 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
Private 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
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, ByVal lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, ByVal lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, ByVal lpdwCurrentDirectory As Long) As Long
Sub Main()
Dim INet As Long
Dim INetConn As Long
Dim RC As Boolean
Dim FileData As WIN32_FIND_DATA
Dim FileList As String
Dim MAX_PATH As Long = 0
INet = InternetOpen("FTP_Backup", 1, vbNullString, vbNullString, 0)
INetConn = InternetConnect(INet, "my.ftp.server.name", 0, "MyUsername", "MyPassword", 1, 0, 0)
RC = FtpGetFile(INetConn, "/notes.ini", "D:\Temp\VB .NET Temp\Notes Data\notes.ini", False, 0, 0, 0)
If RC Then Console.WriteLine("Transferred Notes.ini")
RC = FtpSetCurrentDirectory(INetConn, "/Data/mail")
MsgBox(RC)
RC = FtpGetCurrentDirectory(INetConn, FileList, MAX_PATH)
MsgBox(MAX_PATH)
MsgBox(FileList)
FileData.cFileName = New String("", 0, MAX_PATH)
RC = FtpFindFirstFile(INetConn, "*.*", FileData, 0, 0)
MsgBox(RC)
FileList = FileData.cFileName & ","
'Do While RC = True
' RC = InternetFindNextFile(INetConn, FileData)
' FileList = FileList & FileData.cFileName & ","
' Console.WriteLine(FileList)
'Loop
FileList = Mid(FileList, 1, Len(FileList) - 1)
MsgBox(FileList)
InternetCloseHandle(INetConn)
InternetCloseHandle(INet)
End Sub
End Module
The problem is that FtpSetCurrentDirectory doesn't work (returns False always) and I know that the remote directory exists (I can navigate to it using WS_FTP). Also, FtpGetCurrentDirectory returns True, but the FileList string is empty, the current path is not returned. The FtpGetFile function works just fine, strangely. The FtpFindFirstFile function also returns true, but when I check the values of the FindData structure, it is empty.
Any ideas? This is driving me nuts!
Thanks,
JEB
I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson