我正在尝试使用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 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
如何在没有hacky(例如resource-orders-1437019656813-893.json
)的情况下捕获文件名rightof()
?
答案 0 :(得分:9)
要仅检索文件名列表而不提供其他详细信息,请使用WebRequestMethods.Ftp.ListDirectory
(FTP命令NLST
),而不是WebRequestMethods.Ftp.ListDirectoryDetails
(FTP命令LIST
)。
答案 1 :(得分:3)
这是我使用的功能:
public class FileName : IComparable<FileName>
{
public string fName { get; set; }
public int CompareTo(FileName other)
{
return fName.CompareTo(other.fName);
}
}
public static void getFileList(string sourceURI, string sourceUser, string sourcePass, List<FileName> sourceFileList)
{
string line = "";
FtpWebRequest sourceRequest;
sourceRequest = (FtpWebRequest)WebRequest.Create(sourceURI);
sourceRequest.Credentials = new NetworkCredential(sourceUser, sourcePass);
sourceRequest.Method = WebRequestMethods.Ftp.ListDirectory;
sourceRequest.UseBinary = true;
sourceRequest.KeepAlive = false;
sourceRequest.Timeout = -1;
sourceRequest.UsePassive = true;
FtpWebResponse sourceRespone = (FtpWebResponse)sourceRequest.GetResponse();
//Creates a list(fileList) of the file names
using (Stream responseStream = sourceRespone.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
line = reader.ReadLine();
while (line != null)
{
var fileName = new FileName
{
fName = line
};
sourceFileList.Add(fileName);
line = reader.ReadLine();
}
}
}
}
在main()中设置sourceURI,用户和密码,并声明如下文件列表:
List<FileName> sourceFileList = new List<FileName>();
string sourceURI = "ftp://www.sourceftp.com/";
string sourceUser = "testUser";
string sourcePass = "testPass";
这将为您提供一个易于迭代的文件名列表(sourceFileList [i] .fName)!这些可以使用.Sort()进行排序,您也可以进行二进制搜索。