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