Visual Basic 2008 : Using FTP in the Service (part 1)
Published 8 min readBy Programming4us Editorial Team
Creating FTP Directories
I have created an upload
and download directory beneath the FTP Home Directory, which in my case
is c:\inetpub\ftproot. I have assigned write privileges to the upload
directory and read privileges to the download directory, and set the
anonymous logons to true. Microsoft FTP Publishing Service does not
support authentication over secured sockets, so we will use anonymous
logons.
Adding an FTP Class
Our FTP class is very
simple. It allows for uploading and downloading files, but it lacks more
in-depth features, such as listing directories locally or remotely. For
our purposes, though, it will be sufficient. In this section, we’ll
simulate downloading and processing files from one FTP folder that can
exist anywhere in the world. This could mean moving the file from its
current location to another intranet or extranet location where a
predefined process such as a SQL Task, Web Service, or other mechanism
is in place to process the file further. This procedure works well for
moving data from one data store to another, where a process specific to
that data store has been created to export data to a flat file. In many
cases the systems that create these files have no access to other
intranet or extranet sites and thus have no ability themselves to
directly or indirectly move or process this data on behalf of the remote
data store. Imagine that you have a small Web store application that
uses SQL Server as its back-end data store. Your shipping and receiving,
purchase order information, and customer database is stored in your
back-end mainframe or other secondary centralized SQL Server. If the two
databases can’t communicate—whether because of security issues or
network limitations—you can use an intermediate process, such as a
service that sits on both subnets and has the proper authority to move
the data between network segments or data stores.
We’ll use the FTP class to
read data files from our download directory and store it in our locally
defined folder. In this section, we will do no other processing because
the service will only simulate moving data between network subnets,
allowing a predefined process running on the upload directory’s subnet,
or with access to that subnet, to capture that file and process it.
Let’s review the FTP class, shown in Listing 1.
Listing 1. FTP class implementation.
The <DownloadFiles> Method
<DownloadFiles> is our thread method, started by the <Start> method. It will use the <GetFile>
method to download the files that have been specified in our
modService.vb file. If the file doesn’t download successfully, we will
throw a new exception of System.Exception type. Our Try/Catch will catch
the exception and then write an event into the Application log. If the
file downloads successfully, it will be saved locally in the local
folder, which is specified in the LocalFolder property.
FTP Class Properties
The FTP class has several properties that are required to make our FTP class work. These properties are outlined in Listing 2.
Listing 2. FTP class properties.
The
Uri property represents the physical path and virtual location of the
server and FTP directory where the file that we want to download
resides.
The User property represents the user that we want to log in as. Since we are using anonymous logins, I’ll use anonymous as my user entry.
The
LocalFolder property represents where we will store the files after
downloading them. This can represent a physical local or remote
location.
The Pwd property represents the password used to log on. Since I am using anonymous, any e-mail address will be valid here.
The
Port property represents the port being used by the FTP service. By
default this will be 21 on an FTP server, so I’ll use 21. The File
property represents the name of the file that we want to look for. This
is not a very flexible FTP example. We could extend it to use a wild
card or use the Files property to allow you to pass in an array of file
names to download.