hi friends,
i am trying to access files and folders from any FTP site using its corresponding port.
for example
Input: ftp://dreamincode:21/forums/topic/77912-ftp-download-delete/
from the above FTP site, i need to parse that site and accessing the file which is avail in forums then check it out topic. so i need to access all files and folders till end of the FTP site URL.
files may be
*.log or *.csv like that..
I don't know how to do it, any one can help me to complete this...
thanks in advance,
regards,
sasi
Loading
Zoran HorvatPosted Jul 21, 2011, 8:21 AM
That library can be used in your case like this:
using System;
using System.Collections;
using Utilities.FTP;
namespace FtpTest
{
class Program
{
static void ListCurDirectoryRecursive(FTPclient cli, int indentationLevel)
{
string curDir = cli.CurrentDirectory;
FTPdirectory dirContent = cli.ListDirectoryDetail(curDir);
FTPdirectory subdirs = dirContent.GetDirectories();
Hashtable dirsVisited = new Hashtable();
foreach (FTPfileInfo file in subdirs)
{
Console.WriteLine("{0}{1}", new string(' ', 4 * indentationLevel), file.Filename.ToUpper());
dirsVisited.Add(file.Filename, null);
cli.CurrentDirectory = file.FullName;
ListCurDirectoryRecursive(cli, indentationLevel + 1);
}
foreach (FTPfileInfo file in dirContent)
if (!dirsVisited.ContainsKey(file.Filename))
Console.WriteLine("{0}{1}", new string(' ', 4 * indentationLevel), file.Filename);
}
static void Main(string[] args)
{
Console.Write("Enter host (including ftp:// prefix): ");
string host = Console.ReadLine();
Console.Write("Enter directory to list (directory must start with /): ");
string dir = Console.ReadLine();
Console.Write("Enter user name: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
try
{
FTPclient cli = new FTPclient(host, username, password);
cli.CurrentDirectory = dir;
ListCurDirectoryRecursive(cli, 0);
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("-------------------------------");
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
}
}
However, this library seems to be buggy - I've tried this code on couple of FTP servers and in one case library crashed because FTP server's settings were such that it refused to send file size to the library. So be careful if you don't want to code your own (proper) solution.
Zoran
Zoran HorvatPosted Jul 21, 2011, 6:32 AM
If that does not solve your problems then you have to code your own solution based on socket connection to FTP site. These are the raw FTP commands: http://www.nsftools.com/tips/RawFTP.htm. You can use them to communicate with FTP server over socket.
For exmaple, here is the code which lists FTP directory, but without making distinction between subdirectories and files:
using System;
using System.IO;
using System.Net;
namespace FtpTest
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter FTP url (including ftp:// prefix: ");
string url = Console.ReadLine();
Console.Write("Enter user name: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
try
{
FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(url);
ftpReq.Method = WebRequestMethods.Ftp.ListDirectory;
ftpReq.Credentials = new NetworkCredential(username, password);
using (FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse())
{
using (StreamReader reader = new StreamReader(ftpResp.GetResponseStream()))
{
string response = reader.ReadToEnd();
string[] lines = response.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lines.Length; i++)
Console.WriteLine(lines[i]);
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
}
Zoran