quick ftp question


Jun 7, 2004
Programming Experience

I'm working with wininet.dll functions. My goal is to simply download a file from an ftp site using these functions

Here are declarations for the wininet.dll functions, these functions are declared at the class level (class level being the level of the form)
'This is the internet open function
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

'This is the internet connect function
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, _
ByVal nServerPort As Integer, ByVal sUsername As String, _
ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long

'This file is used to retrieve files
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean

Then I create a button that fires those functions after I have defined them
Dim lngINet As Long
Dim lngINetConn As Long
Dim blnrc As Boolean
Dim strErr As String

lngINet = InternetOpen("FileTransfer", 1, vbNullString, vbNullString, 0)

lngINetConn = InternetConnect(lngINet, "ftp.microsoft.com", 0, _
vbNullString, vbNullString, 1, 0, 0)

blnrc = FtpGetFile(lngINetConn, "index.html", "c:\dirmap.txt", False, 0, 1, 0)
Catch err As Exception
strErr = err.Message
End Try

It is my belief that my code breaks down at the InternetConnect function even though the FtpGetFile returns a false (False meaning the function did not fire), I believe it is returning a false because the first parameter is returning a bad value.

Please help

Quick FTP answer - How to download a file from an ftp server

If you want to download a text file you could try this code:
Sub DownLoadFile(ByVal strURL As String, ByVal strFilePath As String)
    Dim objWebClient As New System.Net.WebClient()
    Dim objWriter As New System.IO.StreamWriter(strFilePath)
    Dim objUTF8 As New System.Text.UTF8Encoding()
    Dim str As String
    str = objUTF8.GetString(objWebClient.DownloadData(strURL))
End Sub

strURL is the complete path to the remote file, strFilePath is the full path of the local file where you want to save the file (c:\dirmap.txt in your example).