使用 ftpwebrequest 列出目录中的所有文件,但它们似乎以 html 格式返回,有什么方法可以让我每行获取一个文件名,而不需要时间戳和所有其他内容?
这是一些代码
StreamWriter fileout = new StreamWriter(@"C:\test\output4.txt");
FtpWebRequest ftp;
string addr = "ftp://blah/";
string usr = "foo";
string pwd = "bar";
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(addr));
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
ftp.Credentials = new NetworkCredential(usr, pwd);
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
fileout.WriteLine(reader.ReadToEnd());
fileout.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
我只是尝试了 printWorkingDirectory 而不是 listDirectory 也没有工作
当然我会添加一些输出顺便说一句我刚刚尝试使用 ftpstream 并得到完全相同的东西。
FileStream outputStream = new FileStream(@"C:\test\output6.txt", FileMode.Create);
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
最后输出
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<TITLE>FTP Listing of /point/download/archive/ at indexftp.barcap.com</TITLE>
<BASE HREF="ftp://foo:bar@indexftp.blah.com/test">
</HEAD>
<BODY>
<H2>FTP Listing of /point/download/archive/ at indexftp.barcap.com</H2>
<HR>
<A HREF="../">Parent Directory</A><BR>
<PRE>
Jul 29 2013 10:00 147280 <A HREF="foo1.csv">foo1.csv</A>
Jul 30 2013 10:04 147079 <A HREF="foo2.csv">foo2.csv</A>
Jul 31 2013 10:05 146935 <A HREF="foo3.csv">foo3.csv</A>
</PRE>
<HR>
</BODY>
</HTML>
其中 foo1, foo2, foo3 位于 ftp 上,我只想要输出
foo1.csv
foo2.csv
foo3.csv