Networked and Distributed
Operating Systems / Project 10- Accessing an FTP Server in C#

Name ______Score ______

In this project we will build and test a Windows Forms application that demonstrates how a C# program can automatically upload and download files to an FTP server.

Introduction - Many applications require the ability to upload and download files via FTP. Even automated processes regularly interact with FTP servers to transfer data. Microsoft has given developers a method to implement this functionality. This project uses methods provided by Microsoft as part of the System.Net namespace. (Excerpts from Use C# to Upload and Download files from an FTP Server - by Zach Smith - )

In order to safely and securely transfer files between computers over the internet, we need to establish some form of authentication. the NetworkCredentials object provided in System.Net does this.

The name and location of the file on the source computer and the intended name and location of the file on the destination computer must be established. When working on your own application that uses FTP access you will need to know something about the directory structure of the server with respect to the entry point provided by the FTP protocol. Note: Because of the use of aliases this may not be obvious.

Downloads - The process of downloading files from an FTP server involves creating a WebClient object to communicate with the server. In the method below a WebClient named request is created, and then an associated NetworkCredentials( ) object is created with the username and password for the target FTP server.

privatevoid download()

{

try

{

//Create a WebClient

WebClient request = newWebClient();

//Setup credentials

request.Credentials = newNetworkCredential(username, password);

//Download data into a Byte array

byte[] fileData = request.DownloadData("ftp://" + host + "/" + filename);

//Create a FileStream and write the Byte array to the file

FileStream file = File.Create(filename);

file.Write(fileData, 0, fileData.Length);

file.Close();

MessageBox.Show("File Download - SUCCESSFUL");

}

catch

{

MessageBox.Show("File Download - FAILED");

}

}

Next, the target file (a textfile in this example) is downloaded using the DownloadData( ) method provided with the request object. The string parameter in this method provides the full path to the target file to download. It is important to note that the URI for the target file and the location of the FTP server are provided at the time the file is downloaded rather than when we provide the Network Credentials.

request.DownloadData("ftp://" + host + "/" + filename);

Once the file is downloaded it is stored on the client (local) machine. This method is for a Windows Forms application so it includes MessageBox'es for indicating whether the download was successful. Note: Other file types use different methods for downloading.

Uploads - The process for uploading files via FTP is a bit different. In this case we create a FtpWeRequest object that includes the URI with intended file name for the file we intend to upload to the server. Also, we have to indicate the method of transfer as Ftp.UploadFile. Setting UsePassive to true, lets your computer send a connection request with requiring a handshake confirmation from the server.

privatevoid upload()

{

try

{

// Get the object used to communicate with the server.

FtpWebRequest request =

(FtpWebRequest)WebRequest.Create("ftp://" + host + "/" + filename);

request.UsePassive = true;

request.Method = WebRequestMethods.Ftp.UploadFile;

// includes username and password

request.Credentials = newNetworkCredential(username, password);

// Copy the contents of the file to the request stream.

StreamReader sourceStream = newStreamReader(filename);

byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

sourceStream.Close();

request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();

requestStream.Write(fileContents, 0, fileContents.Length);

requestStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

response.Close();

MessageBox.Show("File Upload - SUCCESSFUL");

}

catch

{

MessageBox.Show("File Upload - FAILED");

}

}

Since we are sending text files, we encode to UTF8. This step will differ depending on the file types being uploaded. We stream the file to the server and then obtain a response object that we used to close the uploaded file and finish the transfer process.

Project - Create a new Windows Form project called FTPGUI or some appropriate name.

Add using System.Net namespace to your project code. Use the GUI development ToolBox widgets to layout a Form similar to this one.

txtHost - name of Host textbox

txtUsername - name of Username textbox

txtPassword - name of Password textbox

set PasswordChar to *

txtFilename - name of Filename textbox

btnDownload - name of Download Button

btnUpload - name of Upload Button

You will need to create public static strings to hold the values provided in the textboxes. To minimize your workload, use the names already used in the download( ) and upload( ) methods previously discussed.

publicstaticstring host;

publicstaticstring username;

publicstaticstring password;

publicstaticstring filename;

Create button-click events for the Download and Upload buttons. These events should call their respective download and upload methods after ensuring that the appropriate strings have been assigned to host, username, password, and filename from the textboxes. You do not have to "dummy proof" this process for this project.

Testing - An FTP account has been created for us on the CSIS server. Use the account name csc410and the password Murray. The host is csclab.murraystate.edu. Note: downloaded and uploaded files will be in the directory containing your program exe.

1. Download sample.txt from the FTP server. What message does it contain?

______

2. Create and upload your own text file that contains your name. This will be used for grading. What did you name your file?

______

3. Work with another student to exchange text files through the FTP server. Discuss the results.

______

4. Discuss the differences between UDP/TCP and FTP with respect to the programmer.

______

______