ftp列表目录-如何仅获取文件名 [英] Ftp List directory - how to get only filenames
问题描述
我正在为我目前正在编写的一小段代码而苦苦挣扎.该应用程序应该每天运行一次,并从ftp服务器下载所有文件.我的问题是:
I am struggling with a small piece of code which I am currently writing. The application is supposed to run once a day and download all files from an ftp server. My problem is:
尽管从理论上讲,我列出不真实内容的例程运行良好,可以检查所有文件并将其保存到列表中,但实际存在2个错误:
Although in theory my routine to list the direcoty content runs fine, checks all files and saves them to a list, practical there are 2 errors:
- 列表是html格式的
- 我只需要文件名和扩展名
代码
string localPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
List<string> FtpListing = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Properties.Settings.Default.FtpUrl);
//request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
request.Credentials = new NetworkCredential(Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword);
request.Method = WebRequestMethods.Ftp.ListDirectory;
using (StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream()))
{
string fileName = streamReader.ReadLine();
while (fileName != null)
{
FtpListing.Add(fileName);
fileName = streamReader.ReadLine();
}
}
没有代理,它将返回html,而未对代理语句进行注释我正在得到远程服务器返回错误:(550)文件不可用(例如,文件未找到,无法访问).
错误.
Without proxy it returns html, with the proxy statement uncommented I am getting a The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
error.
我在哪里失败?
/这是该列表的屏幕截图,应列出所有文件,但会保存完整的html文件:
/edit: here is a screenshot of the list, where all files should be listed, but instead a complete html file is saved:
推荐答案
我通过使用 HtmlAgilityPack .
由于我无法更改html响应,因此更改了 List< string>FtpListing
转换为简单的 string
.通过此字符串,我解析了webrequest提供的html代码中的每个< a>
标记.
Since I can't change the html response, I changed my List<string> FtpListing
to a simple string
.
With this string I parsed every <a>
tag in the html code provided by the webrequest.
代码:
string HtmlResult = String.Empty;
Console.WriteLine("Starting listing of files....");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Properties.Settings.Default.FtpUrl);
request.Credentials = new NetworkCredential(Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword);
request.UsePassive = false;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (Stream responsestream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responsestream))
{
string _line = reader.ReadLine();
while (_line != null && _line != String.Empty)
{
HtmlResult += _line;
_line = reader.ReadLine();
}
}
}
//parse html output
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(HtmlResult);
foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes("//a[@href]"))
{
if(node.InnerText.Contains(".txt")) FtpListing.Add(node.InnerText);
}
Console.WriteLine("{0} Files found", FtpListing.Count);
这篇关于ftp列表目录-如何仅获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!