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...

相关推荐
山东小木17 分钟前
JBoltAI需求分析大师:基于SpringBoot的大模型智能需求文档生成解决方案
人工智能·spring boot·后端·需求分析·jboltai·javaai·aigs
Moonbit28 分钟前
MoonBit 再次走进清华:张宏波受邀参加「思源计划」与「程序设计训练课」
前端·后端·编程语言
RestCloud30 分钟前
一站式数据集成:iPaaS 如何让开发者和业务人员都满意?
前端·后端·架构
稻草猫.1 小时前
Java多线程(一)
java·后端·java-ee·idea
忧郁的蛋~1 小时前
使用.NET标准库实现多任务并行处理的详细过程
开发语言·c#·.net
Java中文社群1 小时前
炸裂:SpringAI新版发布,终于支持断线重连了!
java·后端·ai编程
sun03221 小时前
使用 javax.net.ssl.HttpsURLConnection 发送 HTTP 请求_以及为了JWT通信选用OSS的Jar的【坑】
http·.net·ssl
哈喽姥爷2 小时前
Spring Boot--Bean的扫描和注册
java·spring boot·后端·bean的扫描和注册
problc2 小时前
Spring Boot `@Service` 互相调用全攻略:`@Autowired` vs `@Resource`
java·spring boot·后端
文心快码BaiduComate2 小时前
文心快码3.5S全新升级,体验多智能体协同开发,最高赢无人机!
前端·后端·程序员