.NET平台超级好用的几款FTP客户端操作库--附代码
FluentFTP
FluentFTP是一个高性能的FTP和FTPS客户端库,专为.NET和.NET Standard环境设计,优化了速度。它提供了广泛的FTP命令支持、文件上传/下载、SSL/TLS连接、自动目录列表解析、文件哈希/校验和、文件权限/CHMOD、FTP代理支持、FXP支持、UTF-8支持、异步/等待(Async/Await)模式、PowerShell集成等功能,完全用C#编写。
安装命令:Install-Package FluentFTP
使用示例
go
```csharp
// 创建FtpClient实例 using (FtpClient client = new FtpClient("00.112.111.11", "zhanghao", "mima111")) {
try { // 连接到FTP服务器 client.AutoConnect(); Console.WriteLine("Connected to FTP server.");
arduino
// 上传文件
client.UploadFile("11.png", "11.png");
Console.WriteLine("File uploaded successfully.");
// 下载文件
client.DownloadFile("123.png", "11.png");
Console.WriteLine("File downloaded successfully.");
// 列出目录内容
FtpListItem[] files = client.GetListing("/2016/03");
foreach (var file in files)
{
Console.WriteLine(file.Name);
}
// 创建目录
client.CreateDirectory("newDirectory");
Console.WriteLine("Directory created successfully.");
// 删除文件
client.DeleteFile("remoteFilePath.txt");
Console.WriteLine("File deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
csharp
### 项目地址
https://github.com/robinrodricks/FluentFTP
## NetFTPLibrary
NetFTPLibrary 是一个功能丰富的FTP库,它支持FTPS(FTP with SSL)、自动重连与恢复、代理支持、ZLIB压缩、目录同步等功能。它同时支持同步和异步操作,适用于多种.NET框架,包括.NET Framework、.NET Core和.NET Standard。
安装命令:`Install-Package NetFTPLibrary`
### 使用示例
```csharp
// FTP服务器的地址、用户名和密码
var server = "ftp.example.com";
var username = "your_username";
var password = "your_password";
// 创建FtpClient实例
using (var ftpClient = new FtpClient())
{
try
{
// 设置FTP服务器的连接信息
ftpClient.Host = server;
ftpClient.Credentials = new NetworkCredential(username, password);
// 连接到FTP服务器
ftpClient.Connect();
// 上传文件
ftpClient.UploadFile("localFilePath.txt", "remoteFilePath.txt");
Console.WriteLine("File uploaded successfully.");
// 下载文件
ftpClient.DownloadFile("remoteFilePath.txt", "localFilePath.txt");
Console.WriteLine("File downloaded successfully.");
// 列出目录内容
var directoryContents = ftpClient.GetListing("/");
foreach (var item in directoryContents)
{
Console.WriteLine(item.Name);
}
// 创建目录
ftpClient.CreateDirectory("newDirectory");
Console.WriteLine("Directory created successfully.");
// 删除文件
ftpClient.DeleteFile("remoteFilePath.txt");
Console.WriteLine("File deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
项目地址
System.Net.FtpClient
System.Net.FtpClient是 是一个简单易用的FTP库,它提供了基本的FTP功能,包括连接、上传、下载、列出目录内容、创建目录和删除文件等。。
使用示例
csharp
// FTP服务器的地址、用户名和密码
var hostname = "ftp.example.com";
var username = "your_username";
var password = "your_password";
// 创建FtpWebRequest对象
var request = (FtpWebRequest)WebRequest.Create(new Uri($"ftp://{hostname}/"));
// 设置登录凭据
request.Credentials = new NetworkCredential(username, password);
try
{
// 连接到FTP服务器
Console.WriteLine("Connecting to FTP server...");
request.Method = WebRequestMethods.Ftp.ListDirectory;
using (var response = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine("Connected to FTP server.");
// 列出目录内容
Console.WriteLine("Directory contents:");
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
// 上传文件
Console.WriteLine("Uploading file...");
var uploadRequest = (FtpWebRequest)WebRequest.Create(new Uri($"ftp://{hostname}/path/to/remote/file.txt"));
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Credentials = new NetworkCredential(username, password);
uploadRequest.UseBinary = true;
using (var fileStream = File.OpenRead("path/to/local/file.txt"))
using (var requestStream = uploadRequest.GetRequestStream())
{
fileStream.CopyTo(requestStream);
}
Console.WriteLine("File uploaded successfully.");
// 下载文件
Console.WriteLine("Downloading file...");
var downloadRequest = (FtpWebRequest)WebRequest.Create(new Uri($"ftp://{hostname}/path/to/remote/file.txt"));
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
downloadRequest.Credentials = new NetworkCredential(username, password);
using (var response = (FtpWebResponse)downloadRequest.GetResponse())
using (var fileStream = File.Create("path/to/local/downloaded/file.txt"))
using (var stream = response.GetResponseStream())
{
stream.CopyTo(fileStream);
}
Console.WriteLine("File downloaded successfully.");
// 创建目录
Console.WriteLine("Creating directory...");
var mkdirRequest = (FtpWebRequest)WebRequest.Create(new Uri($"ftp://{hostname}/newDirectory/"));
mkdirRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
mkdirRequest.Credentials = new NetworkCredential(username, password);
using (mkdirRequest.GetResponse())
{
Console.WriteLine("Directory created successfully.");
}
// 删除文件
Console.WriteLine("Deleting file...");
var deleteRequest = (FtpWebRequest)WebRequest.Create(new Uri($"ftp://{hostname}/path/to/remote/file.txt"));
deleteRequest.Method = WebRequestMethods.Ftp.DeleteFile;
deleteRequest.Credentials = new NetworkCredential(username, password);
using (deleteRequest.GetResponse())
{
Console.WriteLine("File deleted successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
项目地址
FTP_dotNET
FTP_dotNET是一个.NET FTP组件,支持FTP和FTPS, 包括自动目录列表解析,适用于Windows、Unix和Netware平台上的大多数流行FTP服务器。 支持的平台包括.NET Framework 3.5、4、4.5、4.6和4.7。
安装命令:Install-Package FTP_dotNET
使用示例
csharp
// FTP服务器的地址、用户名和密码
var server = "ftp.example.com";
var username = "your_username";
var password = "your_password";
// 创建FtpClient实例
using (var ftpClient = new FtpClient(server, username, password))
{
try
{
// 连接到FTP服务器
ftpClient.Connect();
// 上传文件
ftpClient.UploadFile("path/to/local/file.txt", "path/to/remote/file.txt");
Console.WriteLine("File uploaded successfully.");
// 下载文件
ftpClient.DownloadFile("path/to/remote/file.txt", "path/to/local/file.txt");
Console.WriteLine("File downloaded successfully.");
// 列出目录内容
var directoryContents = ftpClient.GetListing("/path/to/directory");
Console.WriteLine("Directory contents:");
foreach (var item in directoryContents)
{
Console.WriteLine(item.Name);
}
// 创建目录
ftpClient.CreateDirectory("newDirectory");
Console.WriteLine("Directory created successfully.");
// 删除文件
ftpClient.DeleteFile("path/to/remote/file.txt");
Console.WriteLine("File deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
// 断开连接
ftpClient.Disconnect();
}
}