问题描述
我需要通过 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 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!