Recursive upload to FTP server in C#(在 C# 中递归上传到 FTP 服务器)
问题描述
我需要通过 C# 代码将一个文件夹(包含子文件夹和文件)从一台服务器上传到另一台服务器.我做了一些研究,发现我们可以使用 FTP 来实现这一点.但是这样我就只能移动文件而不是整个文件夹.任何帮助在这里表示赞赏.
I would need to upload a folder (which contains sub folders and files) from one server to another from C# code. I have done few research and found that we can achieve this using FTP. But with that I am able to move only files and not the entire folder. Any help here is appreciated.
推荐答案
FtpWebRequest
(也不是 .NET 框架中的任何其他 FTP 客户端)确实没有任何明确支持递归文件操作(包括上传).您必须自己实现递归:
The FtpWebRequest
(nor any other FTP client in .NET framework) indeed does not have any explicit support for recursive file operations (including uploads). You have to implement the recursion yourself:
- 列出本地目录
- 迭代条目、上传文件并递归到子目录(再次列出它们等)
void UploadFtpDirectory(string sourcePath, string url, NetworkCredential credentials)
{
IEnumerable<string> files = Directory.EnumerateFiles(sourcePath);
foreach (string file in files)
{
using (WebClient client = new WebClient())
{
Console.WriteLine($"Uploading {file}");
client.Credentials = credentials;
client.UploadFile(url + Path.GetFileName(file), file);
}
}
IEnumerable<string> directories = Directory.EnumerateDirectories(sourcePath);
foreach (string directory in directories)
{
string name = Path.GetFileName(directory);
string directoryUrl = url + name;
try
{
Console.WriteLine($"Creating {name}");
FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(directoryUrl);
requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
requestDir.Credentials = credentials;
requestDir.GetResponse().Close();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
// probably exists already
}
else
{
throw;
}
}
UploadFtpDirectory(directory, directoryUrl + "/", credentials);
}
}
有关创建文件夹的复杂代码的背景,请参阅:
如何检查FTP目录是否存在
使用如下函数:
string sourcePath = @"C:sourcelocalpath";
// root path must exist
string url = "ftp://ftp.example.com/target/remote/path/";
NetworkCredential credentials = new NetworkCredential("username", "password");
UploadFtpDirectory(sourcePath, url, credentials);
一个更简单的变体,如果您不需要递归上传:
使用WebClient将文件目录上传到FTP服务器
A simpler variant, if you do not need a recursive upload:
Upload directory of files to FTP server using WebClient
或者使用可以自行进行递归的 FTP 库.
Or use FTP library that can do the recursion on its own.
例如,使用 WinSCP .NET 程序集,您只需调用一次即可上传整个目录Session.PutFilesToDirectory
:
For example with WinSCP .NET assembly you can upload whole directory with a single call to the Session.PutFilesToDirectory
:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "username",
Password = "password",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download files
session.PutFilesToDirectory(@"C:sourcelocalpath", "/target/remote/path").Check();
}
Session.PutFilesToDirectory
方法默认是递归的.
The Session.PutFilesToDirectory
method is recursive by default.
(我是 WinSCP 的作者)
这篇关于在 C# 中递归上传到 FTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中递归上传到 FTP 服务器
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01