【发布时间】:2015-10-09 03:26:15
【问题描述】:
我正在尝试使用 StackOverflow 上的以下示例将 FTP 文件夹的内容下载到本地文件夹:
Downloading a list of files from ftp to local folder using c#?
我现在的代码是:
public void DownloadFilesFromFTP(string localFilesPath, string remoteFTPPath)
{
remoteFTPPath = "ftp://" + Hostname + remoteFTPPath;
var request = (FtpWebRequest)WebRequest.Create(remoteFTPPath);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(Username, Password);
request.Proxy = null;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
List<string> directories = new List<string>();
string line = reader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = reader.ReadLine();
}
reader.Close();
using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential(Username, Password);
for (int i = 0; i <= directories.Count - 1; i++)
{
if (directories[i].Contains("."))
{
string path = remoteFTPPath + @"/" + directories[i].ToString();
string trnsfrpth = localFilesPath + @"\" + directories[i].ToString();
ftpClient.DownloadFile(path, trnsfrpth);
}
}
}
response.Close();
}
我收到一个不支持路径的异常,当我检查变量 path 和 trnsfrpth 的值时,它们似乎包含 Apache 信息。
路径: ftp://hostname/data/resourceOrders/-rw-r--r-- 1 apache
apache 367 7 月 16 日 14:07 resource-orders-1437019656813-893.json
和
trnsfrpth: V:\code.runner\local\orders-rw-r--r-- 1 apache apache
367 7月16日 14:07 resource-orders-1437019656813-893.json
如何在没有 hacky(例如rightof())的情况下仅捕获文件名 resource-orders-1437019656813-893.json?
【问题讨论】:
标签: c# .net ftp ftpwebrequest