我正在尝试使用 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 信息。
path: ftp://hostname/data/resourceOrders/-rw-r--r-- 1 apache
apache 367 Jul 16 14:07 resource-orders-1437019656813-893.json
和
trnsfrpth: V:\code.runner\local\orders-rw-r--r-- 1 apache apache
367 Jul 16 14:07 resource-orders-1437019656813-893.json
我如何才能只捕获文件名 resource-orders-1437019656813-893.json
而不使用 hacky(例如 rightof()
)方法?
最佳答案
要仅检索文件名列表而无需其他详细信息,请使用 WebRequestMethods.Ftp.ListDirectory
(FTP 命令 NLST
),而不是 WebRequestMethods.Ftp.ListDirectoryDetails
(FTP 命令 LIST
)。
关于c# - 仅检索 FTP 文件名列表,没有其他详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509170/