我想通过ftp连接到文件夹,并以列表的形式获取文件夹中的文件名。我想比较一下这个列表中的名字和我自己数据库中的名字,看看是否有不同的名字。如何将该文件夹中的文件名作为具有ftp连接和要连接的文件夹信息的列表。
我如何读取和提取名称1.pdf,A.pdf,c4.pdf .等在文件文件夹中的ftp文件夹中作为列表。
例如:文档文件夹包括: 1.pdf,A.pdf,c4.pdf
我想:列出ftpdocumentincludesname=列表();
发布于 2022-02-05 22:47:19
FTP相对较旧,而且实际上仍然提供FTP列表的站点很少,大多数都切换到FTP://一个示例站点,它仍然显示两者都是
FTP://ftp.gnu.org,这取决于它们允许简单视图(如Windows )的时间,很多视图可能只提供HTML样式的接口。

更好的文件将包括自述文件列表,因此不需要自己创建。但如果必须的话,您可以复制和粘贴或“另存为”site_blahblah.htm
如果您需要更好的,有专门的FTP站点浏览器,可以方便地访问和清单。
对于上面这样的简单站点,只需编写一个响应文件,比如pdf.txt
user Anonymous
literal pasv
ls *.pdf
bye并通过命令行ftp -i -s:pdf.txt -n ftp.gnu.org运行它,响应将类似于
Connected to ftp.gnu.org.
220 GNU FTP server ready.
ftp> user Anonymous
230 Login successful.
ftp> literal pasv
227 Entering Passive Mode (209,51,188,20,108,5).
ftp> ls *.pdf
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
blahblah.pdf
blah.pdf
blahblahblah.pdf
226 Directory send OK.
ftp> bye
221 Goodbye.如果您想要更多的详细信息,例如,用ls代替*.pdf,让我们使用真实世界的dir *.gz,我们得到:-
ftp> dir *.gz
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rw-rw-r-- 1 0 3003 240409 Feb 06 21:01 find.txt.gz
-rw-rw-r-- 1 0 3003 462613 Feb 06 21:01 ls-lrRt.txt.gz
-rw-rw-r-- 1 0 3003 543098 Feb 06 21:01 tree.json.gz
226 Directory send OK.对于临时列表,它可以更快地剪切和粘贴文件夹,如上面所示。但是,正如所指出的,您想要使用C#,所以使用股票上市并迭代地解决这个问题。
using System;
using System.IO;
using System.Net;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine($"Directory List Complete, status {response.StatusDescription}");
reader.Close();
response.Close();
}
}
}https://stackoverflow.com/questions/71001676
复制相似问题