Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to List Directory Contents with FTP in C#?

How to List Directory Contents with FTP in C# ?

I am using below code to List Directory Contents with FTP it is returning result in XML format ,but i want only the name of directory not the whole content.

How i Can do that ?

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","[email protected]");          FtpWebResponse response = (FtpWebResponse)request.GetResponse();          Stream responseStream = response.GetResponseStream();         StreamReader reader = new StreamReader(responseStream);         Console.WriteLine(reader.ReadToEnd());          Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);          reader.Close();         response.Close();     } } 

MSDN

like image 448
Swapnil Gupta Avatar asked Jul 21 '10 11:07

Swapnil Gupta


People also ask

How do I list the contents of a folder using FTP?

Use the ls command to list the contents of a directory on the FTP server. Use the dir command to list the contents of a remote directory to the window or to save the output to a local file. Use the lls command to list the contents of the current working directory of your PC or of another directory on the same drive.

What is a directory in FTP?

FTP Get Default Directory is a synchronous activity that retrieves the name of the current remote directory. The default remote directory is operating system dependent and determined by the remote FTP server.


2 Answers

Try this:

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri); ftpRequest.Credentials =new NetworkCredential("anonymous","[email protected]"); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse(); StreamReader streamReader = new StreamReader(response.GetResponseStream());  List<string> directories = new List<string>();  string line = streamReader.ReadLine(); while (!string.IsNullOrEmpty(line)) {     directories.Add(line);     line = streamReader.ReadLine(); }  streamReader.Close(); 

It gave me a list of directories... all listed in the directories string list... tell me if that is what you needed

like image 104
mint Avatar answered Oct 07 '22 22:10

mint


You are probably looking for PrintWorkingDirectory

like image 45
leppie Avatar answered Oct 07 '22 22:10

leppie