■■■

2016年4月5日火曜日

FtpWebRequestでFTPサーバーのファイル一覧を取得する方法

FTPサーバーのファイル一覧を取得する方法
VB.NET
Dim u As New Uri("ftp://localhost/")

Dim ftpReq As System.Net.FtpWebRequest = _
CType(System.Net.WebRequest.Create(u), System.Net.FtpWebRequest)
ftpReq.Credentials = New System.Net.NetworkCredential("username", "password")
ftpReq.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
ftpReq.KeepAlive = False
ftpReq.UsePassive = False

Dim ftpRes As System.Net.FtpWebResponse = _
CType(ftpReq.GetResponse(), System.Net.FtpWebResponse)
Dim sr As New System.IO.StreamReader(ftpRes.GetResponseStream())
Dim res As String = sr.ReadToEnd()
Console.WriteLine(res)
sr.Close()

Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription)
ftpRes.Close()
C#
Uri u = new Uri("ftp://localhost/");

System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)
System.Net.WebRequest.Create(u);
ftpReq.Credentials = new System.Net.NetworkCredential("username", "password");
ftpReq.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;
ftpReq.KeepAlive = false;
ftpReq.UsePassive = false;

System.Net.FtpWebResponse ftpRes =
(System.Net.FtpWebResponse)ftpReq.GetResponse();
System.IO.StreamReader sr =
new System.IO.StreamReader(ftpRes.GetResponseStream());
string res = sr.ReadToEnd();
Console.WriteLine(res);
sr.Close();

Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription);
ftpRes.Close();
■■■