How to Configure FTP Server on Windows

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.

Image 1.jpg

Don't change the server address and port, however you can choose your own password or you can leave it blank. Click on OK.

Image 2.jpg

Go to edit; click on Groups.

Image 3.jpg

Click on "Add"; a popup will appear to enter the group name of your choice and click Ok. Go to edit; click on

Image 4.jpg

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

Image 5.jpg

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 the following namespaces:
  1. using System.Net;  
  2. 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.
  1. int bufferSize = 2048;  
  2. FtpWebRequest ftpRequest =  (FtpWebRequest)FtpWebRequest.Create(@"ftp://127.0.0.1/TESTSITE/hello.pdf");  
  3. ftpRequest.Credentials = new NetworkCredential("nucleotidz""******");  
  4. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;  
  5. ftpRequest.UseBinary = true;  
  6. ftpRequest.UsePassive = true;  
  7. 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:

  1. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;  
There are other methods too like upload file, delete file, rename, create directory etc.

 

  1. FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();  
  2. Stream ftpStream = ftpResponse.GetResponseStream();  
  3. FileStream localFileStream = new FileStream(@"D:\ahmar.pdf", FileMode.Create);  
  4. byte[] byteBuffer = new byte[bufferSize];  
  5. int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);  
  6. try  
  7. {  
  8.     while (bytesRead > 0)  
  9.     {  
  10.         localFileStream.Write(byteBuffer, 0, bytesRead);  
  11.         bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);  
  12.     }  
  13. }  
  14. catch (Exception ex) {  
  15.     Console.WriteLine(ex.ToString());  
  16. }  
  17. localFileStream.Close();  
  18. ftpStream.Close();  
  19. ftpResponse.Close();  
  20. ftpRequest = null;  
  21. Console.Write("ahmar");  
  22. 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.


Similar Articles