NET平台超级好用的几款FTP客户端操作库--附代码

.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);
            }
        }
 

项目地址

www.nuget.org/packages/Ne...

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);
        }
 

项目地址

www.nuget.org/packages/Sy...

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();
            }
        }
 

项目地址

www.nuget.org/packages/FT...

相关推荐
喵叔哟12 分钟前
【.NET 8 实战--孢子记账--从单体到微服务】--简易权限--访问权限中间件
微服务·中间件·.net
man201743 分钟前
【2024最新】基于springboot+vue的闲一品交易平台lw+ppt
vue.js·spring boot·后端
hlsd#1 小时前
关于 SpringBoot 时间处理的总结
java·spring boot·后端
路在脚下@1 小时前
Spring Boot 的核心原理和工作机制
java·spring boot·后端
咩咩觉主1 小时前
尽量通俗易懂地概述.Net && U nity跨语言/跨平台相关知识
unity·c#·.net·.netcore
幸运小圣1 小时前
Vue3 -- 项目配置之stylelint【企业级项目配置保姆级教程3】
开发语言·后端·rust
前端SkyRain2 小时前
后端Node学习项目-用户管理-增删改查
后端·学习·node.js
提笔惊蚂蚁2 小时前
结构化(经典)软件开发方法: 需求分析阶段+设计阶段
后端·学习·需求分析
老猿讲编程2 小时前
Rust编写的贪吃蛇小游戏源代码解读
开发语言·后端·rust