Lesson 34: Creating an FTP Program in VB6

Build a custom FTP client using the Microsoft Internet Transfer Control

Key Takeaway

VB6 allows you to create a fully functional FTP client using the Microsoft Internet Transfer Control with minimal coding.

Welcome to Lesson 34 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to create your own FTP (File Transfer Protocol) client using VB6. This powerful tool enables file transfers between computers over the Internet, perfect for website management or downloading files from FTP servers.

34.1 Understanding FTP

FTP stands for File Transfer Protocol. It's a system for transferring files between two computers connected by the Internet. One computer acts as the server, while the other is the client.

Web Management

Webmasters use FTP to upload files to web servers for website updates

File Downloads

Download free software, games, tools and utilities from FTP sites

Authentication

Use username/password or "anonymous" for public access

34.2 The Microsoft Internet Transfer Control

The engine behind our FTP program is the Microsoft Internet Transfer Control 6.0 (Inet control). To use it:

1Add Control

Press Ctrl+T to open components, select Microsoft Internet Transfer Control 6.0

2Insert Control

Drag the Inet control to your form (default name: Inet1)

3Configure Properties

Set URL, UserName and Password properties

Essential Properties

The Inet control has three crucial properties:

VB6 Code
' Set FTP hostname
Inet1.URL = Text1.Text

' Set username
Inet1.UserName = Text2.Text

' Set password
Inet1.Password = Text3.Text
                        

34.3 Establishing Connection

After setting properties, connect to the server using the Execute method:

VB6 Code
' Connect and list directory contents
Inet1.Execute, "DIR"

' Download a file
Inet1.Execute, , "get" & remotefile & localfile
                        

Important

Use the full local path for file downloads to ensure files are saved correctly:

Inet1.Execute, , "get" & remotefile & localpath & remotefile

34.4 Handling State Changes

Monitor connection status using the StateChanged event and state constants:

Constant Value Description
icHostResolvingHost 1 Looking up IP address of host computer
icHostResolved 2 Successfully found IP address
icConnecting 3 Connecting to host computer
icConnected 4 Successfully connected
icRequesting 5 Sending request to host
icRequestSent 6 Successfully sent request
icReceivingResponse 7 Receiving response from host
icResponseReceived 8 Successfully received response
icDisconnecting 9 Disconnecting from host
icDisconnected 10 Successfully disconnected
icError 11 Error in communication
icResponseCompleted 12 Request completed, all data received

StateChanged Event Code

VB6 Code
Private Sub Inet1_StateChanged(ByVal State As Integer)
    Select Case State
        Case icError
            MsgBox Inet1.ResponseInfo, , "File failed to transfer"
        Case icResolvingHost
            Label6.Caption = "Resolving Host"
        Case icHostResolved
            Label6.Caption = "Host Resolved"
        Case icConnecting
            Label6.Caption = "Connecting Host"
        Case icConnected
            Label6.Caption = "Host connected"
        Case icReceivingResponse
            Label6.Caption = "Receiving Response"
        Case icResponseReceived
            Label6.Caption = "Got Response"
        Case icResponseCompleted
            Dim data1 As String
            Dim data2 As String
            MsgBox "Download Completed"
    End Select
End Sub
                        

34.5 Building the FTP Interface

The FTP program consists of two forms:

1Login Dialog

Accepts FTP address, username and password

2Main Form

Browse remote directories and download files

Login Dialog Interface

Login Dialog Code

Dialog.frm
Option Explicit

Private Sub OKButton_Click()
    Inet1.URL = Text1.Text
    Inet1.UserName = Text2.Text
    Inet1.Password = Text3.Text
    Inet1.Execute , "DIR"
    Form1.Show
    Dialog.Hide
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
    Select Case State
        Case icError
            MsgBox Inet1.ResponseInfo, , "File failed to transfer"
        Case icResolvingHost
            Label6.Caption = "Resolving Host"
        Case icHostResolved
            Label6.Caption = "Host Resolved"
        Case icConnecting
            Label6.Caption = "Connecting Host"
        Case icConnected
            Label6.Caption = "Host connected"
        Case icReceivingResponse
            Label6.Caption = "Receiving Response"
        Case icResponseReceived
            Label6.Caption = "Got Response"
        Case icResponseCompleted
            Dim data As String
            Dim data1 As String
            MsgBox "Transfer Completed"
            data1 = Inet1.GetChunk(1024, icString)
            data = data & data1
            Loop While Len(data1) <> 0
            Form1.Text6.Text = data
    End Select
End Sub

Private Sub CancelButton_Click()
    Text1.Text = ""
    Text2.Text = ""
    Text3.Text = ""
End Sub
                        

Note

The statement data1 = Inet1.GetChunk(1024, icString) retrieves directory information from the remote server.

Main Form Interface

VB6 FTP Main Form
Figure 34.2: FTP Main Form

Lesson Summary

In this lesson, you've learned how to create a functional FTP client in VB6:

Internet Transfer Control

Use the Inet control for FTP capabilities

Connection Setup

Configure URL, username and password properties

State Management

Handle connection states with StateChanged event

File Transfer

Execute FTP commands like DIR and GET

Pro Tip

Enhance your FTP client with features like directory navigation, file uploads, and transfer progress indicators.

Next Lesson

Continue your VB6 journey with Lesson 35: Error Handling.

Related Resources

Microsoft Inet Control

Official documentation on the Internet Transfer Control

View Resource

Advanced FTP Features

Learn to add file uploads, directory creation and more

View Resource

Web Browser App

Previous lesson on creating a web browser

View Lesson

Error Handling

Next lesson on robust error handling

Preview Lesson