Upload directory of files to FTP server using WebClient(使用 WebClient 将文件目录上传到 FTP 服务器)
问题描述
我已经搜索和搜索,但找不到这样做的方法.我在要上传的目录中有文件.文件名不断变化,所以我无法按文件名上传.这是我尝试过的.
I have searched and searched and cannot find a way to do this. I have files in a directory I want to upload. The file names change constantly so I cannot upload by file name. Here is what I have tried.
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("User", "Password");
foreach (var filePath in files)
client.UploadFile("ftp://site.net//PICS_CAM1//", "STOR", @"PICS_CAM1");
}
但我得到一个编译器错误:
But I am getting a compiler error:
当前上下文中不存在名称文件"
The name 'files' does not exist in the current context
我研究过的一切都表明这应该有效.
Everything I have researched says this should work.
有没有人可以通过 WebClient 上传文件目录的好方法?
Does anyone have a good way to upload a directory of files via WebClient?
推荐答案
你必须定义和设置文件.如果您想上传某个本地目录中的所有文件,例如使用 Directory.EnumerateFiles.
You have to define and set the files. If you wanted to upload all files in a certain local directory, use for example Directory.EnumerateFiles.
也是 address 参数noreferrer">WebClient.UploadFile 必须是目标文件的完整 URL,而不仅仅是目标目录的 URL.
Also the address argument of WebClient.UploadFile has to be a full URL to a target file, not just a URL to a target directory.
IEnumerable<string> files = Directory.EnumerateFiles(@"C:localfolder");
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
foreach (string file in files)
{
client.UploadFile(
"ftp://example.com/remote/folder/" + Path.GetFileName(file), file);
}
}
有关递归上传,请参阅:
C#递归上传到FTP服务器
For a recursive upload, see:
Recursive upload to FTP server in C#
这篇关于使用 WebClient 将文件目录上传到 FTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 WebClient 将文件目录上传到 FTP 服务器
基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
