C#使用ftp进行文件上传和下载功能

在C#中,可以使用System.Net命名空间下的FtpWebRequest类来实现FTP文件上传和下载功能。以下是一些示例代码:

FTP文件上传

csharp 复制代码
using System;
using System.IO;
using System.Net;
using System.Text;

public class FtpUploader
{
    public void UploadFile(string localFilePath, string remoteFilePath, string ftpServer, string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + remoteFilePath);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);

        // Convert the file content to a byte array.
        byte[] fileContents = File.ReadAllBytes(localFilePath);

        request.ContentLength = fileContents.Length;

        // Buffer for reading data
        byte[] buffer = new byte[2048];

        // Stream to which the file to be upload is written
        Stream requestStream = request.GetRequestStream();

        // Write Buffer to the File Stream
        requestStream.Write(fileContents, 0, fileContents.Length);
        
        // Close the file stream
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload Complete, status {0}", response.StatusDescription);

        // Close the response.
        response.Close();
    }
}

// 使用示例
class Program
{
    static void Main(string[] args)
    {
        FtpUploader ftpUploader = new FtpUploader();
        string localFilePath = @"C:\path\to\local\file.txt";
        string remoteFilePath = "remote/path/file.txt";
        string ftpServer = "ftp://yourftpserver.com";
        string username = "yourusername";
        string password = "yourpassword";

        ftpUploader.UploadFile(localFilePath, remoteFilePath, ftpServer, username, password);
    }
}

FTP文件下载

csharp 复制代码
using System;
using System.IO;
using System.Net;

public class FtpDownloader
{
    public void DownloadFile(string remoteFilePath, string localFilePath, string ftpServer, string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + remoteFilePath);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(username, password);

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        FileStream localFileStream = new FileStream(localFilePath, FileMode.Create);

        byte[] buffer = new byte[2048];
        int bytesRead = responseStream.Read(buffer, 0, buffer.Length);

        // Write the downloaded data to a local file.
        while (bytesRead > 0)
        {
            localFileStream.Write(buffer, 0, bytesRead);
            bytesRead = responseStream.Read(buffer, 0, buffer.Length);
        }

        // Close the response and stream objects
        response.Close();
        localFileStream.Close();
    }
}

// 使用示例
class Program
{
    static void Main(string[] args)
    {
        FtpDownloader ftpDownloader = new FtpDownloader();
        string remoteFilePath = "remote/path/file.txt";
        string localFilePath = @"C:\path\to\local\file.txt";
        string ftpServer = "ftp://yourftpserver.com";
        string username = "yourusername";
        string password = "yourpassword";

        ftpDownloader.DownloadFile(remoteFilePath, localFilePath, ftpServer, username, password);
    }
}

请注意,在实际使用中,您可能需要处理异常、日志记录以及更复杂的错误处理。此外,如果您处理大文件,您可能想要使用异步方法以避免阻塞UI线程或主线程。

这些示例仅用于演示目的,并且可能需要针对您的特定需求进行调整。在部署到生产环境之前,请确保代码已经经过充分的测试,并且您已经考虑了安全性问题,比如使用安全的FTP连接(例如SFTP或FTPS)。

相关推荐
网络研究院21 分钟前
Proton Drive采用OpenPGP加密,上传速度提升300%
服务器·网络·安全·proton drive·openpgp
Irissgwe24 分钟前
11、五种 IO 模型与阻塞 IO
网络·阻塞·非阻塞·io模型·非阻塞io·异步通信·同步通信
遇见小修修41 分钟前
选择诚信上门修电脑服务,应参考哪些判断标准?
服务器·电脑·负载均衡
世人万千丶1 小时前
鸿蒙PC异常解决:Install Failed: error: failed to install bundle.
服务器·华为·开源·harmonyos·鸿蒙
myenjoy_11 小时前
串口采集与 Modbus RTU——字节流里的时间敏感博弈
网络·python·网络协议·tcp/ip
likerhood1 小时前
服务器使用 vLLM 部署 Qwen2.5-Coder-7B-CL 笔记
服务器·笔记·vllm
dxxt_yy1 小时前
光伏风电组网调试优选,鼎讯信通 GN-W10A 网络综合测试仪全项检测
网络·能源·信息与通信
是枚小菜鸡儿吖1 小时前
IT技术员远程修电脑用什么软件好?低延迟高清远控工具横评
网络·智能路由器·电脑
eam0511231 小时前
BGP反射器及联邦实验
网络
ZFSS1 小时前
BYOK(自带密钥)使用指南
运维·服务器·前端·人工智能·midjourney