In the past few days I have been working with the File Transfer Protocol (FTP) where I tried to configure my own FTP server and download a file from it through C# code. Before I get to the code let me first demonstrate how to configure a FTP server in Windows. Well just use the following link to download FileZilla, an open source FTP server:
http://filezilla-project.org/download.php?type=server
After installation use the following procedure.
1.
Don't change the server address and port, however you can choose your own password or you can leave it blank. Click on OK.
2.
Go to edit; click on Groups.
3.
Click on "Add"; a popup will appear to enter the group name of your choice and click Ok. Go to edit; click on
4.
Click on "Add"; enter the user of your choice; select the group (created in the previous step) from the dropdown.
Check the "Enable account and password" Checkbox and enter a password of your choice. Click on ok.
Your FTP server has been configured.
Address -127.0.0.1
Username-(U created in the 4th step)
Password-( U created in the 4th step)
Port-21
5.
Now the next step is to create a shared folder for saviing all the files. To do that first go to "My Computer" > "D:".
And create a folder called FTPSITE. From your ftpserver go to "Edit" > "Groups" then click on the Shared Folder (left pane second option) then click on "Add"; a pop-up will appear. To choose a folder, navigate to your D: drive and select the FTPSITE folder, click ok and you are done.
Using FileZilla or any FTP client upload files to your FTP server; all files will go into the "D:/FTPSITE".
Since we have configured the FTP server and uploaded files into it, let's start some serious business now. Start Visual Studio and choose console application.
Add Following namespaces:
using System.Net;
using System.IO;
The System.Net namespace has all the classes that we use when writing code for network based applications. In our application we are using FtpWebRequest.
int bufferSize = 2048;
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(@"ftp://127.0.0.1/TESTSITE/hello.pdf");
ftpRequest.Credentials = new NetworkCredential("nucleotidz", "******");
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
Using FtpwebRequest, I am creating a new request and passing the URL ftp://127.0.0.1/TESTSITE/hello.pdf
Because our ftpserver address is 127.0.0.1 and shared folder name is TESTSITE the URL starts with ftp://127.0.0.1/TESTSITE/. The file I uploaded into it is "hello.pdf". I am giving the user name and password in the network credentials that the system will use to connect to the FTP server. Your username and password will be the same that you chose when you configured the server.
As I said earlier, using this code I am downloading a file from the server, therefore you are seeing this:
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
There are other methods too like upload file, delete file, rename, create directory etc.
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(@"D:\ahmar.pdf", FileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
Console.Write("ahmar");
Console.Read();
The next step is to establish and return communication with the server. For that you must use ftpwebresponse in which communication will be returned as a ftpwebresponse object. I am receiving a response stream into a ftpstream.
Once you have a FTP stream and response object, create your local file, read bytes from the FTP stream and write them to your local file. Your file is downloaded.