FTP in .NET Framework
The File Transfer Protocol (FTP) is a standard network protocol used for the transfer of computer files between a client and server on a computer network. .NET Framework provides classes within the System.Net namespace to facilitate FTP operations.
Key Classes for FTP
The primary class for interacting with FTP servers is FtpWebRequest. It allows you to construct and send FTP requests, and FtpWebResponse represents the response from the FTP server.
Common FTP Operations
Uploading a File
To upload a file, you typically create an FtpWebRequest, set the method to FtpMethods.UploadFile, specify the destination URI, and write the file content to the request stream.
using System;
using System.Net;
using System.IO;
public class FtpUploader
{
public static void UploadFile(string ftpHost, string userName, string password, string localFilePath, string remoteFileName)
{
string ftpUrl = $"ftp://{ftpHost}/{remoteFileName}";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(userName, password);
using (Stream fileStream = File.OpenRead(localFilePath))
{
using (Stream ftpStream = request.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}
}
Console.WriteLine("File uploaded successfully.");
}
}
Downloading a File
For downloading, you set the method to FtpMethods.DownloadFile, get the response stream from the FtpWebResponse, and write its content to a local file.
using System;
using System.Net;
using System.IO;
public class FtpDownloader
{
public static void DownloadFile(string ftpHost, string userName, string password, string remoteFileName, string localFilePath)
{
string ftpUrl = $"ftp://{ftpHost}/{remoteFileName}";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(userName, password);
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
using (Stream ftpStream = response.GetResponseStream())
{
using (FileStream fileStream = File.Create(localFilePath))
{
ftpStream.CopyTo(fileStream);
}
}
}
Console.WriteLine("File downloaded successfully.");
}
}
Listing Directory Contents
To get a list of files and directories on the FTP server, use FtpMethods.ListDirectory or FtpMethods.ListDirectoryDetails.
using System;
using System.Net;
using System.IO;
public class FtpLister
{
public static void ListDirectory(string ftpHost, string userName, string password, string remoteDirectory = "/")
{
string ftpUrl = $"ftp://{ftpHost}{remoteDirectory}";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(userName, password);
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}
Important Considerations
- Security: FTP transmits credentials and data in plain text. For secure transfers, consider using FTPS (FTP over SSL/TLS) or SFTP (SSH File Transfer Protocol). While
System.Nethas limited support for FTPS, libraries likeSSH.NETare excellent for SFTP. - Error Handling: Always implement robust error handling using
try-catchblocks to manage potential network issues, authentication failures, or file access problems. - Timeouts: Network operations can be long-running. Configure appropriate timeouts for requests to prevent your application from hanging indefinitely.
- Passive vs. Active Mode:
FtpWebRequestdefaults to passive mode, which is generally more firewall-friendly.