仅检索 FTP 个文件名列表,没有其他详细信息
Retrieving list of FTP file names only, without additional details
我正在尝试使用 Whosebug 上的这个示例将 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
如何在不采用 hacky(例如 rightof()
)方法的情况下仅捕获文件名 resource-orders-1437019656813-893.json
?
要仅检索文件名列表而无需其他详细信息,请使用 WebRequestMethods.Ftp.ListDirectory
(FTP command NLST
), instead of WebRequestMethods.Ftp.ListDirectoryDetails
(FTP 命令 LIST
)。
这是我使用的函数:
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() 进行排序,您也可以进行二进制搜索。
我正在尝试使用 Whosebug 上的这个示例将 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
如何在不采用 hacky(例如 rightof()
)方法的情况下仅捕获文件名 resource-orders-1437019656813-893.json
?
要仅检索文件名列表而无需其他详细信息,请使用 WebRequestMethods.Ftp.ListDirectory
(FTP command NLST
), instead of WebRequestMethods.Ftp.ListDirectoryDetails
(FTP 命令 LIST
)。
这是我使用的函数:
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() 进行排序,您也可以进行二进制搜索。