using System;
using System.Text;
using System.Net;
using System.IO;
namespace ftplist
{
class Program
{
static void Main(string[] args)
{
//substitute with valid ones...
string ftpurl = "ftp://myftpserver.com";
string username = "user";
string password = "password";
Console.WriteLine("Short list from: {0}", ftpurl);
FTPShortList(ftpurl, username, password);
Console.WriteLine("");
Console.WriteLine("Detailed list from: {0}", ftpurl);
FTPDetailedList(ftpurl, username, password);
Console.WriteLine("Press any key to exit...\n");
Console.ReadKey();
}
#region private
private static void FTPShortList(string ftpurl,
string username, string password)
{
FtpWebRequest request = (FtpWebRequest)
WebRequest.Create(ftpurl);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(username,
password);
try
{
FtpWebResponse response = (FtpWebResponse)
request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Directory list complete " +
"with status: {0}", response.StatusDescription);
reader.Close();
response.Close();
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
}
private static void FTPDetailedList(string ftpurl,
string username, string password)
{
FtpWebRequest request = (FtpWebRequest)
WebRequest.Create(ftpurl);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(username,
password);
try
{
FtpWebResponse response = (FtpWebResponse)
request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Directory list complete " +
"with status: {0}", response.StatusDescription);
reader.Close();
response.Close();
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
}
}
|